Created
April 1, 2016 16:09
-
-
Save githubhy/b28b869dc3ce495184cb7fd1ac08e3b6 to your computer and use it in GitHub Desktop.
Understanding what new keyword do and the prototype inheritance.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
function Scope() { | |
this.va = '123'; | |
}; | |
Scope.prototype.va = 'abc'; | |
var s = new Scope(); | |
/* The properties in the prototype can be override */ | |
console.log(s.va); // -> '123' | |
/* The prototype of the newly created object is undefined */ | |
console.log(s instanceof(Scope), s.prototype); // -> true undefined | |
/* Constructor is automatically set to parent */ | |
console.log(s.constructor === Scope); // -> true | |
/* | |
When new Xxx() is called, JavaScript does four things: | |
1. It creates a new object. | |
2. It sets the constructor property of the object to Vehicle. | |
3. It sets up the object to DELEGATE to Xxx.prototype. | |
4. It calls Xxx() in the context of the new object. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment