Using new foo where foo is a new Foo object.

Discussion in 'Javascript' started by blackholebutterfly@gmail.com, Dec 21, 2007.

  1. Guest

    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.
     
    , Dec 21, 2007
    #1
    1. Advertising

  2. 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
     
    Thomas 'PointedEars' Lahn, Dec 21, 2007
    #2
    1. Advertising

  3. slebetman Guest

    On Dec 22, 2:18 am, 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.
     
    slebetman, Dec 22, 2007
    #3
  4. Guest


    > 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?
     
    , Dec 23, 2007
    #4
  5. 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
     
    Thomas 'PointedEars' Lahn, Dec 23, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Allan Schwartz

    new foo -vs- new foo()

    Robert Allan Schwartz, Aug 11, 2004, in forum: C++
    Replies:
    2
    Views:
    355
    marbac
    Aug 12, 2004
  2. Replies:
    3
    Views:
    399
    John Roth
    Jul 29, 2005
  3. .rhavin grobert

    vector: Foo[5] == ((foo*)Foo) + 5 ?

    .rhavin grobert, Sep 23, 2008, in forum: C++
    Replies:
    4
    Views:
    413
    JaredGrubb
    Sep 24, 2008
  4. Brandon Dimcheff
    Replies:
    2
    Views:
    159
    Brandon Dimcheff
    Oct 7, 2008
  5. Replies:
    1
    Views:
    101
    Richard Cornford
    Jul 12, 2007
Loading...

Share This Page