Using new foo where foo is a new Foo object.

B

blackholebutterfly

Why isn't this allowed?

function Foo(){
this.param1 = 'Constructor Function Foo';
}

var foo = new Foo;
var newfoo = new foo;

The last line above gives an error. I thought all objects could be
used as constructors. But it seems the foo object above cannot be used
with the 'new' operator to create more objects. Does anyone know why
that is?
Thanks.
 
T

Thomas 'PointedEars' Lahn

Why isn't this allowed?

function Foo(){
this.param1 = 'Constructor Function Foo';
}

var foo = new Foo;
var newfoo = new foo;

The last line above gives an error. I thought all objects could be
used as constructors. [...]

You thought wrong. Constructors have to be objects that implement the
internal [[Construct]] method which applies to native Function objects and
certain host objects. See the ECMAScript Specification, Edition 3 Final,
sections 11.2.2 and 13.2.


PointedEars
 
S

slebetman

Why isn't this allowed?

function Foo(){
this.param1 = 'Constructor Function Foo';

}

var foo = new Foo;
var newfoo = new foo;

The last line above gives an error. I thought all objects could be
used as constructors. But it seems the foo object above cannot be used
with the 'new' operator to create more objects. Does anyone know why
that is?
Thanks.

Javascript's object inheritence model is prototypal. But for a bunch
of historical reasons the designers of the language decided to
implement a model which tries to emulate classical inheritence (hence
the new keyword). This is why as you've found out "new" works for
"Constructor" objects but not for regular objects.

Other prototypal languages have an "object" operator instead of "new".
Traditionally, the difference between "object" and "new" is that while
"new" creates an object instance from a class (or constructor
functions in javascript) "object" creates an object instance directly
from a live object. Unfortunately the designers of javascript, while
trying to bend it to emulate classical inheritence, neglected to
provide an object operator/function.

Fortunately javascript's prototype model is complete enough that we
can write our own object method:

function object(o) {
function F() {}
F.prototype = o;
return new F();
}

So now you can write:

function Foo(){
this.param1 = 'Constructor Function Foo';
}

var foo = new Foo;
var newfoo = object(foo);

See http://javascript.crockford.com for more on this topic.
 
B

blackholebutterfly

You thought wrong. Constructors have to be objects that implement the
internal [[Construct]] method which applies to native Function objects and
certain host objects. See the ECMAScript Specification, Edition 3 Final,
sections 11.2.2 and 13.2.

Section 13.2 mentions a [[Class]] property, and elsewhere it says it
is "a string value indicating the kind of this object." Does that mean
that if I use the typeof operator only objects of type "Function" can
be used as constructors? How to tell if a given object implements the
internal [[Construct]] method?
 
T

Thomas 'PointedEars' Lahn

You thought wrong. Constructors have to be objects that implement the
internal [[Construct]] method which applies to native Function objects and
certain host objects. See the ECMAScript Specification, Edition 3 Final,
sections 11.2.2 and 13.2.

Section 13.2 mentions a [[Class]] property, and elsewhere it says it
is "a string value indicating the kind of this object." Does that mean
that if I use the typeof operator only objects of type "Function" can
be used as constructors? How to tell if a given object implements the
internal [[Construct]] method?

ISTM you are confusing things. We are discussing the `new' operation, not
the `typeof' operation. (And the `typeof' operation could only result in
"Function" if the operand was a reference to a host object; see section
11.4.3.) I have already named the object types that can be subject to the
`new' operation, and the specification makes it clear that it has to be a
Function object if it is a native object. (However, constructors for other
native objects are Function objects, too; for example RegExp().) If the
object is not a native object that is defined in the specification to
implement [[Construct]], there is no way to tell whether or not it can serve
as a constructor but trial and error. However, by convention even
constructor identifiers for host objects start with a capital letter in
known implementations, for example Image().

Please leave the attribution line in when trimming quotes.


PointedEars
 

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

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top