Why (new RegExp) type is function not object

R

RobG

EME said:
typeof new RegExp

A function is an object. The ECMA spec section 11.4.3 says that the
typeof operator returns 'function' for objects that are functions[1],
and 'object' for objects that aren't functions.

Note that IE doesn't do that, it returns 'object' for functions.


1. It actually says "native and implements [[call]]", but it means
function. :)
 
A

AKS

typeof new RegExp

Actually, only SpiderMonkey creates function, evaluating -new RegExp-
expression. It's more like extension of the engine since the
calculated object has no properties length and prototype.
 
E

EME

for furthur more,i have detail test below:
typeof new RegExp //result 'function' in Firefox, 'object' in IE6
new RegExp instanceof Function //result false in Firefox

(new RegExp)() in Firefox is same as (new RegExp).exec()
 
D

David Mark

for furthur more,i have detail test below:
typeof new RegExp //result 'function' in Firefox, 'object' in IE6

So it has an internal [[call]] method in Firefox.
new RegExp instanceof Function //result false in Firefox

It isn't an instance of Function (it is an instance of RegExp.) Just
because an object returns "function" as its type, doesn't mean it is
an instance of Function. Some host objects return "function" as their
type (e.g. document.images in Safari), which means they are callable,
but certainly they are not Functions.
 
T

Thomas 'PointedEars' Lahn

David said:
It isn't an instance of Function (it is an instance of RegExp.)

It isn't an instance of anything, as that would imply class-based inheritance.


PointedEars
 
D

David Mark

It isn't an instance of anything, as that would imply class-based inheritance.

You are kidding right? The operator quoted above that sentence should
explain why I used that terminology.
 
T

Thomas 'PointedEars' Lahn

David said:
You are kidding right?

I am not.
The operator quoted above that sentence should explain why I used that terminology.

The operator above was borrowed from Java, a programming language that
uses only class-based inheritance, to ease programming in JavaScript for
developers that a familiar with Java. However, `instanceof' is only a
name, not a hint regarding the underlying concept of the operation in
implementations of Edition 3 of the ECMAScript Language Specification.
It is therefore inappropriate to speak of objects as instances of their
constructors.

http://developer.mozilla.org/en/doc...ls_of_the_Object_Model#Summary_of_Differences


PointedEars
 
D

David Mark

I am not.

That was a rhetorical question. I know you are serious about being
pedantic.
The operator above was borrowed from Java, a programming language that
uses only class-based inheritance, to ease programming in JavaScript for

No kidding.
developers that a familiar with Java. However, `instanceof' is only a
name, not a hint regarding the underlying concept of the operation in

I wasn't taking a hint from it.
implementations of Edition 3 of the ECMAScript Language Specification.
It is therefore inappropriate to speak of objects as instances of their
constructors.

Thanks for that.
 
J

John G Harris

It isn't an instance of anything, as that would imply class-based inheritance.

Let's be really pedantic now. "An instance of RegExp" is just short for

"The object is a member of the class of all those objects that could
possibly be constructed by the function named RegExp".


Let's be differently pedantic now. Here is a quote from the ECMAScript
standard :

"RegExp instances inherit properties" ...


John
 
R

Richard Cornford

RobG said:
On Dec 8, 9:15 am, Thomas 'PointedEars' Lahn wrote: [...]
It is therefore inappropriate to speak of objects as
instances of their constructors.

Thanks for that.

Maybe the instanceof operator should be "constructedby"? :)

The - instanceof - operator makes an assertion about the runtime
relationship between the objects on an object's prototype chain and the
object/value referred to by the - prototype - property of a function at
the moment of the operation. Because the - prototype - properties of
functions can be assigned values at any time the results of -
instanceof - do not necessarily represent anything beyond the outcome of
the pertinent algorithms.

As usual, if the 'Class' concept has been employed in javascript code
design, not violated at runtime, and no attempt has been made to extend
that concept across possible multiple frames (so there is only a single
instance of each 'constructor') then javascript's - instanceof - can be
employed as if it was broadly analogous to Java's - instanceof -.
Unfortunately the coincidence of names tends to give people familiar
with Java the false impression that the two operators are more related
than they actually are (and then they whinge when they find out that the
relationship in javascript is much more specific and concrete than the
conceptual relationship that is implied in Java).

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top