prototypal-inheritance

Convert javascript class instance to plain object preserving methods

孤街醉人 提交于 2019-12-22 06:37:18
问题 I want to convert an instance class to plain object, without losing methods and/or inherited properties. So for example: class Human { height: number; weight: number; constructor() { this.height = 180; this.weight = 180; } getWeight() { return this.weight; } // I want this function to convert the child instance // accordingly toJSON() { // ??? return {}; } } class Person extends Human { public name: string; constructor() { super(); this.name = 'Doe'; } public getName() { return this.name; } }

Why use chained prototype inheritance in javascript?

房东的猫 提交于 2019-12-20 19:41:49
问题 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

Javascript Prototypal Inheritance?

最后都变了- 提交于 2019-12-20 10:32:45
问题 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

Javascript Prototypal Inheritance & object properties shadowing

与世无争的帅哥 提交于 2019-12-20 10:09:29
问题 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

Javascript functional inheritance with prototypes

会有一股神秘感。 提交于 2019-12-20 09:24:31
问题 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 =

Javascript functional inheritance with prototypes

守給你的承諾、 提交于 2019-12-20 09:21:38
问题 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 =

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

∥☆過路亽.° 提交于 2019-12-20 07:26:04
问题 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(

Difference between constructor in ES6 class and constructor in prototype?

六眼飞鱼酱① 提交于 2019-12-20 04:58:07
问题 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

Override Methods with Prototypal Inheritance

纵然是瞬间 提交于 2019-12-20 03:47:09
问题 I'm using this clone method for prototypal inheritance from Pro JavaScript Design Patterns which is basically the same as Crockford's object() function. (The only difference is that Crockford adds invoke parens, but since F is empty I'm not sure that it matters. I don't think that the issue.) clone = function(object) { function F() {} F.prototype = object; return new F; }; So, applying this, I'm looking to make two objects, one which inherits methods from the other. One is for viewport

(Open Source) Examples of JavaScript Prototypical OO

♀尐吖头ヾ 提交于 2019-12-18 10:02:38
问题 Bounty Edit: I'm looking for code written in a pure prototypical OO paradigm (think Self). Not a mixture of prototypical OO and classical OO. I don't want to see generic OO wrappers but simply usage of prototypical OO techniques and only prototypical OO techniques. Reference Related Question: Prototypical OO in JavaScript In the above question I mainly focused on Can write prototypical OO like this? Do we need constructors and initialization logic, What are the alternatives? New question: