Sunday, 28 May 2017

Node.js Global Scope

Below is list of debugger queries, and responses, executed in a node.js at the global scope level:

this.toString()
    "[object Object]"
Object.getPrototypeOf(this).toString()
    "[object Object]"
Object.getPrototypeOf(Object.getPrototypeOf(this))
    null
this.constructor
    function Object() { [native code] }
Object.prototype.toString()
    "[object Object]"
Object.prototype.toString.call(this)
    "[object Object]"

Function.prototype.toString()
    "function () {}"
Function.prototype.toString.call(this)
    TypeError: Function.prototype.toString is not generic

The first set of responses suggests that the global scope is represented by an object that extends Object.prototype, as might be expected. So I'd expect that calling Function.prototype's .toString() on this would fail, as indeed it does (see the last query). I was expecting the query preceding that to pass an implicit this and so fail likewise, but it returns function () {}.

Why the difference, and what function is being referenced in the response?



via Martin

No comments:

Post a Comment