Reputation: 615
When considering this simple example
<script>
function test()
{
this.x = 10;
}
var obj = new test();
</script>
I read in on one of the blogs that when we use a new keyword , a proto object would be created and "this" would be set to proto object.
So, when I just call var obj = test();
, will the "this" be set to proto object or will the proto object not created at all in this scenario?
So, what would be the basic difference from a programmer's point of view between the two calling methods?
Upvotes: 2
Views: 210
Reputation: 10538
The new
statement creates a new instance (object) which basically inherits from the constructors prototype
. However, the function's prototype (__proto__
in most environments) has nothing to do with the instances' prototype. If not used as a constructor (i.e. without new
), the function will use different object context, in this case the global one (in hosting environments like the web browsers, this would be window
). From MDN:
var o = new Object();
o.[[Prototype]] = Foo.prototype;
About the object context: more details. About the prototype and functions: more details.
Update In relation to the Rob's comment below: context and scope are not the same. Every function invocation has both a scope and a context associated with it. Fundamentally, scope is function-based while context is object-based. In other words, scope pertains to the variable access of a function when it is invoked and is unique to each invocation. Context is always the value of the this keyword which is a reference to the object that “owns” the currently executing code. (More details here.)
Upvotes: 1
Reputation: 147413
I read in on one of the blogs that when we use a new keyword , a proto object would be created and "this" would be set to proto object.
If that's what it said, it was wrong.
If the new operator is used when calling a function, the function's this parameter is set to a new Object created as if by new Object()
. This object has its internal [[Prototype]]
set to the constructor's public prototype object.
It also causes the function to return this new object (and instance of the constructor) if no other object is returned by a return statement, so:
function test() {
this.x = 10;
}
var obj = new test();
Creates a new object with a public x property with value 10
and internal [[Prototoype]]
property that references test.prototype. A reference to this new object is assigned to obj.
So, when I just call var obj = test(); , will the "this" be set to proto object or will the proto object not created at all in this scenario?
You mean like:
var obj = test();
In this case, test is called without setting its this, so it will default to the global object (or be undefined in strict mode), so that:
this.x = 10;
creates an x property of the global object (if it didn't already exist) and assigns it the value of 10
. This effectively creates a global variable, with subtle differences to one created using a variable declaration in the global execution context.
Upvotes: 2
Reputation: 2084
var obj = new test();
this would set x= 10 in the context of 'obj' only. That means after above statement if you do
console.log(obj.x); //output will be 10
console.log(x) //output will be error
Now, when you do:
var obj = test();
this would set x=10 in the execution context (global prototype). That means after above statement if you do:
console.log(obj.x); //output will be error
console.log(x) //output will be 10
Upvotes: 4