Last active
August 29, 2015 14:05
-
-
Save justinbmeyer/348d04203a9539dcff13 to your computer and use it in GitHub Desktop.
DOT and NEW operator
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
var Person = function(n) {ame) { | |
this.name = name; | |
} | |
Person.prototype.isPerson = true; | |
var person = new Person('Smith'); | |
var DOT = function(obj, prop) { | |
if( obj.hasOwnProperty(prop) ) { | |
return obj[prop]; | |
} | |
else if(obj.__proto__) { | |
return DOT(obj.__proto__, prop); | |
} | |
} | |
DOT(person, 'name'); // person.name | |
DOT(person, 'isPerson'); // person.isPerson |
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
var Person = function(name) { | |
this.name = name; | |
} | |
Person.prototype.speak = function(){ console.log('Hello') } | |
var NEW = function(konstructor, args) { | |
var o = {}; | |
o.__proto__ = konstructor.prototype; | |
var result = konstructor.apply(o, args); | |
if(result && typeof result === 'object') return result; | |
return o; | |
} | |
var person = NEW( Person, ['name'] ); //new Person('name') | |
person.speak(); // -> 'Hello' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment