prototypal-inheritance

Implementing inheritance in node.js bindings

南楼画角 提交于 2019-12-05 01:13:12
问题 I am writing Node.js bindings around a C++ library. I can identify key objects in the C++ library that I can expose as classes to the Node.js (i.e. derivatives of ObjectWrap). I can also see an inheritance relationship between these objects. How can I expose ClassA , ClassB , ClassC as node.js classes (derivatives of ObjectWrap ) and manipulate their prototypes (in v8 C++ code) so that ClassB and ClassC are derivates of ClassA ? 回答1: This can be done using v8::FunctionTemplate 's Inherit

Javascript Inheritance ; call and prototype

假装没事ソ 提交于 2019-12-04 13:56:49
问题 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

Confused about JavaScript prototypal inheritance

杀马特。学长 韩版系。学妹 提交于 2019-12-04 08:46:06
问题 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

Prototype and constructor in JavaScript (plain English)?

无人久伴 提交于 2019-12-04 08:27:37
问题 "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

In Javascript, the difference between 'Object.create' and 'new'

点点圈 提交于 2019-12-04 08:12:41
问题 I think the difference has clicked in my head, but I'd just like to be sure. On the Douglas Crockford page Prototypal Inheritance in JavaScript, he says In a prototypal system, objects inherit from objects. JavaScript, however, lacks an operator that performs that operation. Instead it has a new operator, such that new f() produces a new object that inherits from f.prototype. I didn't really understand what he was trying to say in that sentence so I performed some tests. It seems to me that

How to create a JS object with the default prototype from an object without a prototype?

自古美人都是妖i 提交于 2019-12-04 03:48:53
问题 Background: The module query-string is for example able to parse key=value&hello=universe to an object {key: 'value', hello: 'universe'} . However, the module author has decided that the returned object does not have a prototype. In other words, this "bastard" object is created by Object.create(null) . Problem: It would be convenient to use parsed.hasOwnProperty('hello') but that is not possible without the default object prototype. Of course, one could Object.prototype.hasOwnProperty.call

Can JavaScript constructor return function and keep inheritance?

一个人想着一个人 提交于 2019-12-03 19:51:53
问题 function F() { return function() { return {}; } } var f = new F(); f instanceof F; // returns false As far as I understand, if I want instanceof to work, I need to return this from the constructor. But I want the constructor to return a function, and I cannot assign to this . So, is this really impossible or can it be done somehow, for f = new F() to return a function and still f instanceof F to return true? 回答1: function F() { var r = function() { return {}; }; r.__proto__ = this.__proto__;

Implementing inheritance in node.js bindings

亡梦爱人 提交于 2019-12-03 17:36:28
I am writing Node.js bindings around a C++ library. I can identify key objects in the C++ library that I can expose as classes to the Node.js (i.e. derivatives of ObjectWrap). I can also see an inheritance relationship between these objects. How can I expose ClassA , ClassB , ClassC as node.js classes (derivatives of ObjectWrap ) and manipulate their prototypes (in v8 C++ code) so that ClassB and ClassC are derivates of ClassA ? This can be done using v8::FunctionTemplate 's Inherit method. It's explained here . Here's a working example. C++ code: #include <v8.h> #include <node.h> using

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

只谈情不闲聊 提交于 2019-12-03 12:57:26
问题 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

Why was JavaScript implemented using prototypal inheritance?

主宰稳场 提交于 2019-12-03 11:55:39
问题 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. 回答1: 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