Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Are global functions methods of the global object?

Reply
Thread Tools

Are global functions methods of the global object?

 
 
User1014
Guest
Posts: n/a
 
      12-01-2006
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?

http://developer.mozilla.org/en/docs...ning_Functions
doesn't actually give any insight.
 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      12-01-2006
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.

 
Reply With Quote
 
 
 
 
User1014
Guest
Posts: n/a
 
      12-01-2006
* 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?
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      12-01-2006
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.

 
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
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
Why Petshop Changed all static methods to instance methods when upgrading from version 3.0 to version 3.1? Neo ASP .Net 1 01-07-2005 01:46 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments