Created
February 2, 2017 23:34
-
-
Save fanyer/5e1cca67257746f0eeb5d6f3fa500f88 to your computer and use it in GitHub Desktop.
This file contains 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
function Rocker(name){ | |
this.name = name; | |
}; | |
Rocker.prototype.getName = function(){ | |
return this.name; | |
} | |
//https://rockjins.github.io/2017/01/18/new-object/ | |
var createObject = function(){ | |
var obj = new Object(), //(1) | |
Constructor = [].shift.call(arguments); //(2) | |
obj.__proto__ = Constructor.prototype; //(3) | |
var ret = Constructor.apply(obj, arguments); //(4) | |
return typeof ret === 'object' ? ret : obj; //(5) | |
}; | |
var shock = createObject(Rocker, 'Shock'); | |
console.log(shock.name); //Shock | |
console.log(shock.getName()); //Shock | |
console.log(Object.getPrototypeOf(shock) === Rocker.prototype); //true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment