nodejs vs ecma - global execution context

98 views
Skip to first unread message

Szymon Piłkowski

unread,
Apr 8, 2011, 7:08:26 PM4/8/11
to nod...@googlegroups.com
Hi all,

From what I understand after reading some ECMA-262 specs, global
execution context - the object where 'this' keyword points when you're
in the outermost scope - should be the 'global' object containing
built-in language global methods/objects (things like Math, isNaN and
so on), enriched with environment-specific methods/objects (such as
setTimeout, document etc in the browser).
In a browser, such object is also called 'window'.

However, in Node it looks quite strange:

[wildcard@magic-missile bookbu.js (master)]$ node
> this
{}
> require('util').inspect(this)
'{}'
> this+""
'[object global]'

... however:

> for (var i in this) { console.log(i) }
global
process
GLOBAL
root
setTimeout
setInterval
clearTimeout
clearInterval
console
Buffer
module
require
_
i

notice that we're still not seeing everythig. where's Math?

> Math
{}
> this.Math
{}
> Math === this.Math
true
> Math+""
'[object Math]'

hm. cool. 'enumerable' property from ECMAScript 5 would explain something, but:

> Object.getOwnPropertyDescriptor(this)
{ value: undefined, writable: true, enumerable: false, configurable: false }
> Object.getOwnPropertyDescriptor(Math)
>

now I'm lost. phantom objects everywhere! but hey...

> for (var i in Math) { console.log(i) }
> Math.PI
3.141592653589793

now I'm lost^2.
some objects show their members, some others don't. some of them list
their properties, some don't. it's possible to iterate over members of
some object (but not all of them), and see some of their members (but
not all of them).

anyone can help me with some explanation? I understand that Node may
not follow ECMA strictly in some cases, but it just doesn't make any
sense to me.

PS. node v0.4.0

--
Szymon Piłkowski :-: http://twitter.com/ard

Qing Guan

unread,
Apr 8, 2011, 9:56:38 PM4/8/11
to nod...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.

As far as I understood about javascript, not all global objects are belong to 'this'.
There are some good article about js, check it out may help you understand javascript better:

mscdex

unread,
Apr 8, 2011, 10:07:07 PM4/8/11
to nodejs
On Apr 8, 7:08 pm, Szymon Piłkowski <szymon.pilkow...@gmail.com>
wrote:
> anyone can help me with some explanation? I understand that Node may
> not follow ECMA strictly in some cases, but it just doesn't make any
> sense to me.

Do this in a REPL and you'll see all global variables:
Object.getOwnPropertyNames(global);
or: Object.getOwnPropertyNames(Math);

`for(var x in global)` will only show enumerable properties, whereas
getOwnPropertyNames will show non-enumerable properties as well.

Oleg Slobodskoi

unread,
Apr 9, 2011, 4:10:19 AM4/9/11
to nod...@googlegroups.com
consider also if you are inside of a module this points to exports object.

2011/4/9 Szymon Piłkowski <szymon.p...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
>
>

--
regards,
Oleg Slobodskoi
--------------------------------------------------------------------
Xing: https://xing.com/profile/Oleg_Slobodskoi
Twitter: https://twitter.com/oleg008
Github: https://github.com/kof

Reply all
Reply to author
Forward
0 new messages