prototypal-inheritance

Setting Object.prototype.__proto__ instead of just Object.prototype?

你离开我真会死。 提交于 2019-12-18 07:10:10
问题 I am looking at this article regarding the node.js events module: http://www.sitepoint.com/nodejs-events-and-eventemitter/ And in it there is this code: Door.prototype.__proto__ = events.EventEmitter.prototype; Which supposedly sets the prototype of the Door object to the prototype of the event.EventEmitter. I believe I know what is the difference between prototype and proto but this code completely confuses me. So my questions is whether instead of using: Door.prototype.__proto__ = events

How to create private variable accessible to Prototype function?

為{幸葍}努か 提交于 2019-12-17 17:38:24
问题 I'm trying to get a deeper hold on prototypal inheritance and class creation (I know, there are other ways, but for the purpose of this I'm trying to grasp prototypes.) My question is: Using the following code example, is there a way to create private variables inside of Tree and Fruit that will not be returned with the function, but is still accessible to the prototype functions genus and bulk ? var Tree = function ( name, size ) { this.name = name; this.size = size; }; Tree.prototype.genus

Performing inheritance in JavaScript

倾然丶 夕夏残阳落幕 提交于 2019-12-17 06:26:34
问题 Now while I know that you can not perform inheritance like you would in C#, I have seen it mentioned on the Internet that it is kind of possible. If it's not possible using plain JavaScript code then would it be possible using Ext JS and if so how? 回答1: The JavaScript object oriented paradigm is prototype based. There are no "classes", just objects. You can implement inheritance in different ways. The two more popular alternatives are the "pseudo-classical" and the "prototypal" forms. For

Why use Object.prototype.hasOwnProperty.call(myObj, prop) instead of myObj.hasOwnProperty(prop)?

那年仲夏 提交于 2019-12-17 02:41:13
问题 If I understand correctly, each and every object in Javascript inherits from the Object prototype, which means that each and every object in Javascript has access to the hasOwnProperty function through its prototype chain. While reading require.js' source code, I stumbled upon this function: function hasProp(obj, prop) { return hasOwn.call(obj, prop); } hasOwn is a reference to Object.prototype.hasOwnProperty . Is there any practical difference to writing this function as function hasProp(obj

Critique my prototypal inheritance pattern [closed]

冷暖自知 提交于 2019-12-13 22:31:01
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago . I've decided to use Object.create, as it seems much more intuitive than using 'new' and having to write Car.prototype.func1 = function(){} for each function, for example; seems a bit too DRY. I had an epiphany of using $.extend to augment properties and functions, making it easier

What is the alternative equivalent of Javascript “prototype” in Typescript?

本秂侑毒 提交于 2019-12-13 08:59:21
问题 Typescript provides Object Oriented & Generic programming paradigms apart from Functional programming offered by Javascript. The keyword prototype is a very powerful and sometimes a dangerous tool. In general, I read that it simulates the inheritance aspect in Javascript. So preferably, what is the [closest] alternative of prototype in .ts? 回答1: Typescript provides Object Oriented & Generic programming paradigms apart from Functional programming offered by Javascript. No, JavaScript itself

The constructor of Object.prototype

落花浮王杯 提交于 2019-12-13 03:57:48
问题 In JavaScript, every object inherits its properties and methods from a specific prototype, where prototypes are objects. The inheritance forms a prototype chain where (Object.prototype) stands at its top (followed by null which has no properties or methods) and all the objects inherit from it (unless someone else inserts other changes to the prototype chain). If (Object.prototype) is an object, what is its constructor? I mean what completes this expression in order to be evaluated to true.

Javascript ES6 Classes composition

耗尽温柔 提交于 2019-12-13 03:23:46
问题 i'm struggling in what would be a good practice or better approach to communicate 'sibling classes in es6' quoted because they haven't a real parent class, by definition. let me explain better: class Car { constructor(typeOfMotor){ this.motor = typeOfMotor; this.mount(); this.addListener(); } mount() { // Some async logic here, and this will return true or false; } addListener(driver) { // Here i want to listen this.mount method and, // when return true, then call the ride method in the

Why Function.prototype cannot be modified ?

笑着哭i 提交于 2019-12-13 02:12:06
问题 in the Function.prototype page it's written this : Function objects inherit from Function.prototype. Function.prototype cannot be modified. Or in javascript there are no classes but the Inheritance and the prototype chaining in which constructors are actually functions : function AclassName(){ return 2; } // AclassName ---> Function.prototype ---> Object.prototype ---> null and i think it's always possible to extend the class prototype's like : AclassName.prototype.color = "somevlue"; So what

resetting the constructor property of prototype object

有些话、适合烂在心里 提交于 2019-12-13 00:16:10
问题 Consider the following snippet: function shape(){ this.name = "2d shape" } function triangle(){ this.name = "triangle"; this.ttest = function(){ alert("in triangle constructor"); } } function equitriangle(){ this.name = "equitriangle" } var s = new shape(); triangle.prototype = s; equitriangle.prototype = new triangle(); var et = new equitriangle(); alert(et.name); // this alerts equitriangle et.ttest(); // this alerts in triangle constructor alert(x.isPrototypeOf(et));// this alerts true