I'm not understanding how this
in the BigObject constructor can be undefined
when not using the new
keyword -- see examples below. Given the following code snippet in Firebug:
( function( global ){
"use strict";
var fromunderbutter = "fun";
global.BigObject = function( options ){
console.log( this );
console.log( this instanceof BigObject );
};
})( this );
The following code makes sense:
>>> var x = new BigObject();
>>> Object { } // new constructor creates blank object context that is assigned to BigObject
>>> true // BigObject was the object context ( this ) that invoked BigObject()
From what i understand, this
refers to the current object context. In the above example because of the new keyword, this
will refer to a blank object which will be newly created and applied to the function invocation.
But this next part doesn't make sense to me:
>>> BigObject()
>>> undefined
>>> false
Why is this
undefined? I assumed that this
would refer to something -- probably global object window. Not sure how to think about this result.
Thanks