Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Using new foo where foo is a new Foo object.

Reply
Thread Tools

Using new foo where foo is a new Foo object.

 
 
blackholebutterfly@gmail.com
Guest
Posts: n/a
 
      12-21-2007
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.
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      12-21-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
 
 
 
slebetman
Guest
Posts: n/a
 
      12-22-2007
On Dec 22, 2:18 am, (E-Mail Removed) wrote:
> 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.
 
Reply With Quote
 
blackholebutterfly@gmail.com
Guest
Posts: n/a
 
      12-23-2007

> 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?
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      12-23-2007
(E-Mail Removed) wrote:
>> 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
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between Foo = Class.new and class Foo Brandon Dimcheff Ruby 2 10-07-2008 03:16 PM
vector: Foo[5] == ((foo*)Foo) + 5 ? .rhavin grobert C++ 4 09-24-2008 01:57 PM
why functions in modules need 'global foo' for integer foo but not dictionary foo? seberino@spawar.navy.mil Python 3 07-29-2005 12:42 PM
Regex combining /(foo|bar)/ slower than using foreach (/foo/,/bar/) ??? Gunnar Hjalmarsson Perl Misc 12 02-24-2005 08:46 PM
new foo -vs- new foo() Robert Allan Schwartz C++ 2 08-12-2004 07:23 AM



Advertisments