Are global functions methods of the global object?

Discussion in 'Javascript' started by User1014, Dec 1, 2006.

  1. User1014

    User1014 Guest

    User1014, Dec 1, 2006
    #1
    1. Advertising

  2. User1014 wrote:
    > A global variable is really just a property of the "Global Object", so
    > what does that make a function defined in the global context? A method
    > of the Global Object?


    Global functions are also properties of the global object. That does
    make them methods of that object in the broad sense but for a function
    to deserve the label 'method' it should be using the - this - keyword
    to reference the object instance that it is a method of (or possibly
    some other (scope chain held reference to that object instance), and in
    javascript the value of the - this - keyword is determined by how a
    function is called and not related to any structure of assignments of
    function references to properties of objects.

    Whenever the mechanism for determining the value of - this - cannot
    assign a specific value to - this - it assigns a reference to the
    global object instead. The practical effect is that any function that
    is not called as a method of another object is going to be executed as
    what is effectively a method of the global object, so for the ones that
    are methods of the global object (in the sense of being referred to by
    properties of the global object) that distinction is arbitrary.

    Richard.
     
    Richard Cornford, Dec 1, 2006
    #2
    1. Advertising

  3. User1014

    User1014 Guest

    * Richard Cornford wrote:
    > User1014 wrote:
    >> A global variable is really just a property of the "Global Object", so
    >> what does that make a function defined in the global context? A method
    >> of the Global Object?

    >
    > Global functions are also properties of the global object. That does
    > make them methods of that object in the broad sense but for a function
    > to deserve the label 'method' it should be using the - this - keyword
    > to reference the object instance that it is a method of (or possibly
    > some other (scope chain held reference to that object instance), and in
    > javascript the value of the - this - keyword is determined by how a
    > function is called and not related to any structure of assignments of
    > function references to properties of objects.


    say I have:

    function sayBoo() {
    alert ('boo!');
    }

    this function could be called via

    sayBoo();

    or

    this.sayBoo();

    or even

    window.sayBoo();

    is that right?

    Are you saying that invoking it via the this.sayBoo() only works because
    'this' cannot determine any object and so binds by default to the Global
    object and therefore behaves as though sayBoo() is a method but in fact
    actually isn't?

    If I sound a bit confused it's because I am ;-) could you provide an
    example if what I've said is incorrect?
     
    User1014, Dec 1, 2006
    #3
  4. User1014 wrote:
    > * Richard Cornford wrote:
    > > User1014 wrote:
    > >> A global variable is really just a property of the "Global Object", so
    > >> what does that make a function defined in the global context? A method
    > >> of the Global Object?

    > >
    > > Global functions are also properties of the global object. That does
    > > make them methods of that object in the broad sense but for a function
    > > to deserve the label 'method' it should be using the - this - keyword
    > > to reference the object instance that it is a method of (or possibly
    > > some other (scope chain held reference to that object instance), and in
    > > javascript the value of the - this - keyword is determined by how a
    > > function is called and not related to any structure of assignments of
    > > function references to properties of objects.

    >
    > say I have:
    >
    > function sayBoo() {
    > alert ('boo!');
    > }
    >
    > this function could be called via
    >
    > sayBoo();
    >
    > or
    >
    > this.sayBoo();
    >
    > or even
    >
    > window.sayBoo();
    >
    > is that right?
    >
    > Are you saying that invoking it via the this.sayBoo() only works
    > because 'this' cannot determine any object and so binds by
    > default to the Global object


    The value of the - this - keyword is determined when you call a
    function, at the point of calling a function (except in the global
    execution context, which is never called as such), so it is not that -
    this - cannot determine any object, but rather the mechanism that
    decides what - this - should be that considers assigning - this - to a
    specific object, and used the global object otherwise.

    As written you - this.sayBoo() - appears to be executing inline in the
    global execution context so - this - is a reference to the global
    object and sayBoo is being executed as a method of the global object.

    > and therefore behaves as though sayBoo() is a method but in fact
    > actually isn't?


    That comes back to whether a reference to a function being a property
    of an object makes that function into a method of that object. Your
    sayBoo function is referred to by a property of the global object, and
    in that sense it is a method of the global object. As it never used the
    - this - keyword that distinction is irrelevant.

    > If I sound a bit confused it's because I am ;-) could you provide an
    > example if what I've said is incorrect?


    But an example of what?

    If you write - sayBoo() - the evaluation of the function call involves
    resolving - sayBoo - into what ECMA 262 calls a 'Reference' type (or
    into a value that is a reference to a function, in which case the -
    this - value will always be the global object), which is an object with
    two properties, a 'base' property that may be Null or may be a
    reference to an object and a 'propertyName' property that is a string.
    The nature of this Reference type, and specifically the value of its
    'base' property, determines which value will be assigned to the - this
    - keyword inside the resulting function call.

    If the 'base' value is not Null _and_ the object it refers to is not an
    "Activation" object then the - this - value will be a reference to that
    object, otherwise it will be a reference to the global object. (The
    global object is never an "Activation" object, though it is a
    "Variable" object for the global execution context. In function
    exaction contexts the "Activation" object is used as the "Variable"
    object).

    In javascript Identifiers and property accessors evaluate to Reference
    types. Identifiers (such as unqualified global function names such as -
    sayBoo - ) are resolved against the scope chain, by examining the
    objects in the chain until one is found with a property that has a name
    that corresponds with the Identifier. When a property is found on an
    object in the chain the resulting Reference type will have a reference
    to that object assigned to its 'base' property, and that object
    reference will become the - this - value, if, and only if, that object
    is not an "Activation" object (otherwise the global object will be
    used). If no object is found on the scope chain with the named property
    then the 'base' property of the Reference type is assigned Null, and
    then the global object will be used for the - this - reference.

    So, given - sayBoo() -, - sayBoo - is resolved against the scope chain
    and as the global object is on the scope chain and has a property named
    'sayBoo' the resulting Reference type has the global object assigned to
    its 'base' property. As the global object is not an "Activation" object
    the - this - value for the execution of the function can then be set to
    the value of the ' base' property of the Reference type; the global
    object. Thus in this case the value of the - this - keyword can be
    explicitly set to the global object instead of defaulting to the global
    object.

    With a property accessor such as - window.sayBoo - first the Identifier
    - window - is resolved against the scope chain, the resulting Reference
    type used to retrieve a value that is a reference to an object, and
    then a Reference type is produced by looking up the - sayBoo -
    Identifier as a property of that first object. The resulting reference
    type has its 'base' property set to a reference to that object, which
    is the object referred to by the global - window - property, and is
    also a reference to the global object. Again this object (the global
    object) is explicitly used as the - this - value for the function call
    instead of the value being defaulted to a reference to the global
    object.

    The practical upshot of which is that it still doesn't matter much
    whether a global function is called as a method or called as a global
    function, because the - this - keyword is still going to refer to the
    global object and there is no more to being a 'method' in javascript
    than being able to use the - this - keyword to refer to the object that
    the function is a 'method' of (assuming that the method is
    appropriately called; that the Reference type has the significant
    object instance as its 'base' property).

    Richard.
     
    Richard Cornford, Dec 1, 2006
    #4
    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. Xiangliang Meng
    Replies:
    1
    Views:
    1,615
    Victor Bazarov
    Jun 21, 2004
  2. Replies:
    4
    Views:
    391
    Howard
    Jun 14, 2005
  3. flamesrock
    Replies:
    6
    Views:
    407
    bruno modulix
    Jun 7, 2005
  4. Nikita Petrov
    Replies:
    2
    Views:
    114
    Gary Wright
    Apr 6, 2008
  5. Kenneth McDonald
    Replies:
    5
    Views:
    326
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page