javascript-inheritance

Confusion With Javascript Inheritance

人走茶凉 提交于 2019-12-24 06:01:38
问题 I'm confused about javascript inheritance. Consider the following code: function parent(firstname, lastname) { this.firstname = firstname || "abc"; this.lastname = lastname || "def"; } function child() { this.childname = "xys"; } parent.prototype.Greetings = function () { alert("sayhi"); } child.prototype = Object.create(parent.prototype); var child1 = new child(); Now, does the child1 object have access to the firstname and lastname properties? I can access the Greetings method (because it's

Confusion With Javascript Inheritance

时光怂恿深爱的人放手 提交于 2019-12-24 06:00:08
问题 I'm confused about javascript inheritance. Consider the following code: function parent(firstname, lastname) { this.firstname = firstname || "abc"; this.lastname = lastname || "def"; } function child() { this.childname = "xys"; } parent.prototype.Greetings = function () { alert("sayhi"); } child.prototype = Object.create(parent.prototype); var child1 = new child(); Now, does the child1 object have access to the firstname and lastname properties? I can access the Greetings method (because it's

Javascript sub classing, super constructors and using custom extend loses base class methods

落爺英雄遲暮 提交于 2019-12-13 05:26:19
问题 In another SO question about whether to call a super-constructor or use the prototype chain the answer provided by one of the users seemed sensible but didn't work for me when I implemented it on my solution which has multiple layers of inheritance. Here's the answer I'm referring to: https://stackoverflow.com/a/4389429/392591 So I've created a jsFiddle using this custom extend method to illustrate the problem: https://jsfiddle.net/68q7yghv/ I've added the jsFiddle code at the bottom. In my

Understanding the __extends function generated by typescript?

爷,独闯天下 提交于 2019-11-30 04:13:57
I am playing with Typescript and trying to understand the compiled Javascript code generated by the compiler Typescript code: class A { } class B extends A { } Generated Javascript code: var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b

Understanding the __extends function generated by typescript?

空扰寡人 提交于 2019-11-28 23:10:58
问题 I am playing with Typescript and trying to understand the compiled Javascript code generated by the compiler Typescript code: class A { } class B extends A { } Generated Javascript code: var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b);

How to extend Function with ES6 classes?

人盡茶涼 提交于 2019-11-26 06:57:58
ES6 allows to extend special objects. So it's possible to inherit from the function. Such object can be called as a function, but how can I implement the logic for such call? class Smth extends Function { constructor (x) { // What should be done here super(); } } (new Smth(256))() // to get 256 at this call? Any method of class gets reference to the class instance via this . But when it is called as a function, this refers to window . How can I get the reference to the class instance when it is called as a function? PS: Same question in Russian. Bergi The super call will invoke the Function

How to extend Function with ES6 classes?

强颜欢笑 提交于 2019-11-26 01:58:07
问题 ES6 allows to extend special objects. So it\'s possible to inherit from the function. Such object can be called as a function, but how can I implement the logic for such call? class Smth extends Function { constructor (x) { // What should be done here super(); } } (new Smth(256))() // to get 256 at this call? Any method of class gets reference to the class instance via this . But when it is called as a function, this refers to window . How can I get the reference to the class instance when it