12
Jan
Posted by admin » Add Comment »
JavaScript rule number one
Forget everything you know about OOP.
var mammal = {getName:function(){console.log("I am a mammal")}};
var Cat = function() {this.getName = function() {console.log("I am a cat")}};
Cat.prototype = mammal;
var Man = function() {this.getName = function() {console.log("I am a man")}}
Man.prototype = mammal;
luke = new Man();
luke.getName();
>>> I am a man
luke instanceof Cat
>>> true
If you are thinking “WTF???” you are wrong. JavaScript objects are decoupled from constructors and instanceof is just a unlucky naming to say something as simple as:
luke.__proto__ == Cat.prototype;
There's 0 Comment So Far
Share your thoughts, leave a comment!