Skip to content

Instantly share code, notes, and snippets.

@githubhy
Created April 1, 2016 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save githubhy/b28b869dc3ce495184cb7fd1ac08e3b6 to your computer and use it in GitHub Desktop.
Save githubhy/b28b869dc3ce495184cb7fd1ac08e3b6 to your computer and use it in GitHub Desktop.
Understanding what new keyword do and the prototype inheritance.
'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