YOU DONT KNOW JS PROTOTYPAL INHERITANCE CODE
Many thanks to who pointed out my previous code was wrong, giving a huge boost to the reported speed of the prototype method. What we did above is Classical Inheritance using prototype chaining, and this is also called Prototypal Inheritance as people think it is ok to add a qualifier like Classical or Prototypal to an age old term Inheritance and think it is ok. You Dont Know JS: this & Object Prototypes: Simpson, Kyle. Here, JS is made to behave like Java by coercing a prototype relationship. Reference References are the idea that two or more variables are pointing at the same value, such that modifying this shared value would be reflected by access via any of those references. prototypal inheritance under the hood, and could actually cause more problems than it might solve. References In Chapter 2: Surveying JS, we discussed the different kinds of values: primitives and objects. instanceof goes hand in hand with the JavaScript function-as-constructor + prototypal inheritance paradigm. I guess when speed matters, though, I don't have a choice. You Dont Know JS Book Series: Like other books in this series, You Don’t Know JS: ES6 & Beyond dives into trickier parts of the language that many. If you are trying to buy into the school of thought in JS that you should not use new, then you are opting out of a lot of the language features, including instanceof. I like my object code to be self-encapsulated, and not allowed to drift. It's a real shame, because I really hate using prototype. This.showMsg = function(string) Ĭonsole.log((new Date()).getTime() - intNow) //FF=5206ms Safari=1554Ĭonsole.log((new Date()).getTime() - intNow) //FF=3894ms Safari=606 Then I read in this John Resig's article "Instantiating a function with a bunch of prototype properties is very, very, fast", but is he talking about using prototype in the standard way, or is he talking about his specific example in his article?įor example, is creating this object: function Class1() Though this is also a downside because as we saw, its so different than classes in other languages, we still use the old “Prototypal Inheritance” with a lot of layers of abstractions over it.I read here (Douglas Crockford) using prototype operator to add methods to Javascript classes saves also memory.
The syntax is also closer to other languages and it looks like an easy thing to learn. JavaScript is a bit confusing for developers experienced in class-based languages (like Java or C++), as it is dynamic and does not provide a class implementation per se (the class keyword is. You Dont Know JS : ES6 and Beyond Go to book. Classes shines the most when we do “inheritance”, we just use the extends key word and invoking super() instead of jumping hoops with the other patterns.
Though it becomes tedious to do deep level chaining. It use links, which works as that one object can essentially delegate property/function access to another object. Inheritance implies a copy operation, but JavaScript don’t copy object properties at all. Especially where performance is concerned as well as the need for this with.