prototypal-inheritance

Confused about JavaScript prototypal inheritance

我是研究僧i 提交于 2019-12-03 00:46:57
In the book " JavaScript the definitive guide 5 edition ", section 9.2 Prototypes and Inheritance, I find the following words: In the previous section, I showed that the new operator creates a new, empty object and then invokes a constructor function as a method of that object. This is not the complete story, however. After creating the empty object, new sets the prototype of that object. The prototype of an object is the value of the prototype property of its constructor function. All functions have a prototype property that is automatically created and initialized when the function is

Prototype and constructor in JavaScript (plain English)?

丶灬走出姿态 提交于 2019-12-02 22:58:01
"JavaScript is the worlds most misunderstood language" -D.Crockford My questions: Constructor and prototypes in plain English? What is the need of using a prototype? What is the purpose behind using Prototypes and constructors? I mean do they provide more flexibility. I am asking this as I have been using this language for the past six months and never had a situation where I used prototypes and constructor. I am not looking for any syntax and how to go about things explanations as I do understand some part of what they are, just wanted to know these things in a simpler way. An analogy (non

Javascript Prototypal Inheritance?

落花浮王杯 提交于 2019-12-02 22:52:14
I'been doing some inheritance in js in order to understand it better, and I found something that confuses me. I know that when you call an 'constructor function' with the new keyword, you get a new object with a reference to that function's prototype. I also know that in order to make prototypal inheritance you must replace the prototype of the constructor function with an instance of the object you want to be the 'superclass'. So I did this silly example to try these concepts: function Animal(){} function Dog(){} Animal.prototype.run = function(){alert("running...")}; Dog.prototype = new

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

大城市里の小女人 提交于 2019-12-02 21:35:51
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 somewhere on SO that described how to find out just this but I can't for the life of me find it again.

Javascript Prototypal Inheritance & object properties shadowing

只谈情不闲聊 提交于 2019-12-02 21:11:02
var person = { name :"dummy", personal_details: { age : 22, country : "USA" } }; var bob = Object.create(person); bob.name = "bob"; bob.personal_details.age = 23; console.log(bob.personal_details === person.personal_details); // true : since it does not shadow object of prototype object console.log(bob.name === person.name); // false : since it shadows name ////now bob.personal_details = {a:1}; console.log(bob.personal_details === person.personal_details); //false When object bob tries to override " name " property of person then, it gets shadowed in bob itself. But in case of personal_details

Prototypal inheritance in JavaScript

好久不见. 提交于 2019-12-02 21:10:28
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? The reason is that using Hoozit.prototype = Gizmo.prototype would mean that modifying Hoozit's prototype object would also modify objects

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

China☆狼群 提交于 2019-12-02 19:36:11
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)are always looked up in the prototype chain? - Private variables are always looked up in the scope chain

Javascript functional inheritance with prototypes

大城市里の小女人 提交于 2019-12-02 18:26:12
In Douglas Crockford's JavaScript: The Good Parts he recommends that we use functional inheritance. Here's an example: var mammal = function(spec, my) { var that = {}; my = my || {}; // Protected my.clearThroat = function() { return "Ahem"; }; that.getName = function() { return spec.name; }; that.says = function() { return my.clearThroat() + ' ' + spec.saying || ''; }; return that; }; var cat = function(spec, my) { var that = {}; my = my || {}; spec.saying = spec.saying || 'meow'; that = mammal(spec, my); that.purr = function() { return my.clearThroat() + " purr"; }; that.getName = function()

javascript riddle: 2 objects that seem identical with respect to constructor, prototype and __proto__ link, behave differently

こ雲淡風輕ζ 提交于 2019-12-02 11:44:46
I am an experienced object oriented programmer but this got me! Why am I able to do new f() but not new a(). I will appreciate any pointers. // first a few facts if (Object instanceof Function) console.log("Object isa Function"); console.log("Function.prototype is " + Function.prototype); /* output Object isa Function Function.prototype is function Empty() {} */ var f = new Function(); console.log("Prototype of f:" + f.prototype); console.log("Constructor of f:" + f.constructor); console.log("Prototype Link of f:" + f.__proto__); if (f instanceof Function) console.log("f isa Function"); /*

Difference between constructor in ES6 class and constructor in prototype?

懵懂的女人 提交于 2019-12-02 06:25:47
Both ES6 class and prototype of function have a contructor , but I'm wondering are they the same? Let me give more explanations. So, I create a Cat function, for instance: const Cat = function (name) { this.name = name; }; The Cat has the following prototype: This constructor can be lost if I type smth. like Cat.prototype = {}; , but new Cat('Name'); will continue working. Ang we have the following syntax in ES6: class Dog { constructor(name) { this.name = name; } } The class also has constructor and it looks just like a simple function. Since classes are just a syntactic sygar over prototype