prototypal-inheritance

Javascript Inheritance ; call and prototype

独自空忆成欢 提交于 2019-12-03 08:44:33
To implement inheritance in Javascript, one generally does the following 2 steps; Say I have a base class "Animal" var Animal = function(name){ this.name = name; } I now want to derive a sub class "Dog" from the same. So I would say var Dog = function(name) { Animal.call(this,name); } So I am calling my parent class constructor from my derived class constructor. The 2nd step is to set the prototype as follows; Dog.prototype = new Animal(); Now I can access any of the base "Animal" class properties from within my derived class Dog. So my question is why are these 2 steps necessary ? If we just

What is the difference between an object and a prototype in prototypal programming?

别等时光非礼了梦想. 提交于 2019-12-03 07:46:36
问题 I'm trying to understand the "JavaScript way" of creating and using objects and I think I'm running into a misunderstanding of an object and a prototype. In a new project I've started I've decided to try out prototypal inheritance. I'm confused if this means I should just create an object that I intend to use and then create other objects based on that using Object.create() such as: var labrador = { color: 'golden', sheds: true, fetch: function() { // magic } }; var jindo = Object.create(dog)

Prototypal inheritance in JavaScript

倾然丶 夕夏残阳落幕 提交于 2019-12-03 07:42:21
问题 I've been watching Douglas Crockford's talks at YUI Theater, and I have a question about JavaScript inheritance... Douglas gives this example to show that "Hoozit" inherits from "Gizmo": function Hoozit(id) { this.id = id; } Hoozit.prototype = new Gizmo(); Hoozit.prototype.test = function (id) { return this.id === id; }; Why does he write Hoozit.prototype = new Gizmo() instead of Hoozit.prototype = Gizmo.prototype ? Is there any difference between these two? 回答1: The reason is that using

How can I see a Javascript object's prototype chain?

空扰寡人 提交于 2019-12-03 06:59:39
问题 Given the following code: function a() {} function b() {} b.prototype = new a(); var b1 = new b(); We can stay that a has been added to b 's prototype chain. Great. And, all the following are true: b1 instanceof b b1 instanceof a b1 instanceof Object My question is, what if we don't know the origins of b1 ahead of time? How can we discover the members of its prototype chain? Ideally I'd like an array like [b, a, Object] or ["b", "a", "Object"] . Is this possible? I believe I've seen an answer

Why use chained prototype inheritance in javascript?

放肆的年华 提交于 2019-12-03 06:17:51
perf Why do we build a prototype inheritance chain rather then using object composition. Looking up through the prototype for each step in the chain get's expensive. Here is some dummy example code : var lower = { "foo": "bar" }; var upper = { "bar": "foo" }; var chained = Object.create(lower, pd(upper)); var chainedPrototype = Object.create(chained); var combinedPrototype = Object.create(pd.merge(lower, upper)); var o1 = Object.create(chainedPrototypes); var o2 = Object.create(combinedPrototypes); uses pd because property descriptors are verbose as hell. o2.foo is faster then o1.foo since it

Scope chain look-up vs prototype look-up - Which is when

旧城冷巷雨未停 提交于 2019-12-03 06:10:19
问题 If a variable is not available in a function when it's needed, then it's being looked for in the scope chain (which is a closure), but other times it's being searched for in the prototype chain. I am trying to wrap my head around which is happening when. I was wondering if someone could kindly clear the mist for me, or refer me to some literature discussing this topic specifically. For example, would I be right saying that: - Objects and therefore public variables tied to the context (this

Extjs: extend class via constructor or initComponent?

天涯浪子 提交于 2019-12-03 05:45:51
问题 In extjs you can always extend an extjs class via the constructor() . For classes derinving from Component you can also extend via initComponent() . I am wondering why so many code extend via initComponent , whereas constructor seems to be the universal extension method. Does initComponent offer clear advantage over constructor ? 回答1: First off, the ability to override via constructor was added in a later version of Ext than initComponent , so all code of a certain age would have to use

why does listing the actual constructor of a class in javascript important

感情迁移 提交于 2019-12-03 03:08:46
I was reading on javascript garden http://bonsaiden.github.com/JavaScript-Garden/ about prototype in javascript and one of its example goes like this: function Foo() { this.value = 42; } Foo.prototype = { method: function() {} }; function Bar() {} // Set Bar's prototype to a new instance of Foo Bar.prototype = new Foo(); Bar.prototype.foo = 'Hello World'; // Make sure to list Bar as the actual constructor <------------------- Bar.prototype.constructor = Bar; Notice the line that reads Make sure to list Bar as the actual constructor. I really am lost about what this does/is. I have tried making

Why was JavaScript implemented using prototypal inheritance?

。_饼干妹妹 提交于 2019-12-03 02:16:09
There are lots of articles and posts explaining how JavaScript inheritance works, but why was JavaScript implemented using prototypal inheritance instead of classical inheritance? I love JavaScript, so I'm not saying it's bad thing... I'm just curious. Here's what Brendan Eich has to say about what happened: http://weblogs.mozillazine.org/roadmap/archives/2008/04/popularity.html As I've often said, and as others at Netscape can confirm, I was recruited to Netscape with the promise of "doing Scheme" in the browser. At least client engineering management including Tom Paquin, Michael Toy, and

What are patterns you could use with prototype inheritance that you cannot with class?

柔情痞子 提交于 2019-12-03 02:00:17
Everyone seems to generally agree that prototype inheritance is simpler and more flexible than class inheritance. What I have not seen in the literature that I've read is very many examples of things that you can do with prototype inheritance that you cannot with classical. So I pose a simple question: What are some patterns that you can use with prototype inheritance that you cannot with class inheritance and what is the guidance you would give as far when/if to use it? One difference (perhaps at least conceptually) is that class inheritance implies that the child IS-A type of the parent.