Loading ...
Sorry, an error occurred while loading the content.

Re: Execution Scope and Functions/Objects/Classes/etc

Expand Messages
  • Michael Hasenstein
    I must add still one more thing - since this is a serious error in Javascript itself. The constructor property is NOT part of the created object, which one
    Message 1 of 25 , Dec 4, 2008
    • 0 Attachment
      I must add still one more thing - since this is a serious error in Javascript itself. The constructor property is NOT part of the created object, which one would logically expect (it would be nice and obvious if every object's constructor property would point to the function that really "constructed" this object).

      Instead, the constructor property *is inherited*. It really is on the constructor Function object's prototype object. In my (previous) example, where F is the constructor function:

      F.prototype.constructor === F
      "true"

      Since "a" (below) was constructed by F it has a secret link (accessible in Firefox  but not in e.g. IE or Opera) to F's prototype object, and an attempt to access a.constructor - as I did in the example below - therefore finds that property, even though it is NOT in "a" itself, but Javascript keeps looking in the secret-link object and finds it there.

      That works fine in this simple 1-level inheritance example. Unfortunately it fails if there are more inheritance levels, where the constructor property inherited points to the wrong function instead of the one that actually created that particular object.

      Oh and as a tip, don't ever confuse the inheritance in Javascript provided by that "secret link" with the "prototype" property. The latter is a property only Function objects possess, and it is meant only for constructor functions - but given to all functions simply because each one just might be(come) a constructor. The secret link, __proto__ in Firefox, is set by the Javascript engine itself, and it is set to point to the prototype property of the Function object used to construct the object. That's Object.prototype for objects created using literal notation. The Javascript engine than uses that secret link to look for properties it cannot find in the object itself. The prototype property it points to is an object itself - and if that object itself was created using a constructor it too has a secret link.


      > There's also a property called constructor. It points to the actual
      > constructor function itself:
      >
      > a.constructor === F
      > "true"
      >
      > and
      >
      > b = {}
      > b.constructor === Object
      > "true"
      >
      > (unless the user overwrites the property) object.constructor.prototype
      > points to the same object as __proto__ but is official and works in all
      > Javascript implementations.
      >
      > Among other things, YUI uses these to build a property superclass when
      > using YAHOO.lang.extend. You can also use it to implement your own
      > "instanceof" functionality. However, only the (JS interpreter
      > implementation specific) secret link property is used to look for
      > functions in the inheritance chain by the JS interpreter.
    Your message has been successfully submitted and would be delivered to recipients shortly.