prototypal-inheritance

Prototype chain in Javascript not updated

[亡魂溺海] 提交于 2019-12-11 03:59:52
问题 I'm trying to understand prototypal inheritance in Javascript, but failing to apply to the following case. Any help would be appreciated. I'm defining a constructor as follows: var base = function() { var priv = "private"; // Private var publ = "public"; // Public through a getter/setter (below) // The object to return var f = {}; f.publ = function (new_val) { if (!arguments.length) { return publ; } publ = new_val; return f; }; return f; }; With this constructor, I can create objects by

How to prevent direct access to properties when inheriting from a base provider?

老子叫甜甜 提交于 2019-12-11 02:39:35
问题 This is a follow up to How to inherit from base provider (not the provider factory)?. The proposed solution suggests a combination of angular.extend and angular.copy (which can be done with just angular.merge on Angular 1.4) to copy the base provider implementation into all other providers. But this led to another issue that I was not expecting. With this technique, my providers are now configurable through the provider.setX function, along with direct access to provider.config.x property.

Illegal Invocation on addEventListener

前提是你 提交于 2019-12-10 20:11:30
问题 With jQuery you can use .on() / .off() / .trigger() methods on any jQuery object, giving you powerful access to the event system. I'm trying to do something similar in vanilla JavaScript but I keep running into "TypeError: Illegal Invocation" . I'm aware this error generally refers to losing a this reference when the method expects one. Using .apply or .call seems to usually do the trick, but I'm still running into problems. Specifically, I'm trying to create an event enabled "class", which I

Javascript - how to initialize super class if super class constructor takes arguments

点点圈 提交于 2019-12-10 18:16:38
问题 Consider the following Javascript snippet, var SuperClass = function(x){ this.x = x; this.a = 5; }; var SubClass = function(x){ } function IntermediateClass(){}; IntermediateClass.prototype = SuperClass.prototype; SubClass.prototype = new IntermediateClass; SubClass.prototype.constructor = SubClass; Now If i create an instance of SubClass, the object will not initialize the property "a" and "x". var object = new SubClass; //object.a == undefined //object.x == undefined While I understand why

In Backbone.js how can I get Model superclass defaults to act as defaults for subclasses?

狂风中的少年 提交于 2019-12-10 03:14:40
问题 I have a class that defines some defaults, and a subclass that defines some defaults. But when I create an instance of the subclass it only looks at the local defaults and does not merge its defaults with those of the parent. Is there any simple way to do this without explicitly merging the local defaults with the parent defaults in the initialize function of every subclass? var Inventory = Backbone.Model.extend({ defaults: { cat: 3, dog: 5 } }); var ExtendedInventory = Inventory.extend({

class use in a prototypal inheritance-based language

孤者浪人 提交于 2019-12-10 02:25:07
问题 The following answer to this question does a great job explaining the differences between classical inheritance and prototypal inheritance . this was of interest to me to understand because I started working in Java, but moved over to Javascript. In his answer, he states for prototypal inheritance that, "All of the business about classes goes away. If you want an object, you just write an object." Yet there is so much documentation and questions on how to "write classes" in Javascript. Why is

Does the ECMAScript specification allow Array to be “superclassable”?

别说谁变了你拦得住时间么 提交于 2019-12-09 08:12:28
问题 I'm looking for any indications whether or not "superclassing" a builtin type will work according to the specification . That is, given any hypothetical conformant implementation of ECMAScript, does "superclassing" a builtin break the runtime by affecting the creation algorithm of the class constructor? "Superclassable" , a term I'm coining, refers to a class whose objects returned by constructing it, or calling it as a function if applicable, will be created with the same internal slots

Safely inheriting prototypes in JavaScript [duplicate]

左心房为你撑大大i 提交于 2019-12-08 06:13:27
问题 This question already has answers here : Benefits of using `Object.create` for inheritance (4 answers) Closed 5 years ago . Let's say I'm shooting for some basic inheritance in my application, I could achieve this by setting the prototype of my child to the parent. // Parent "class" var Car = function(year) { this.year = year; }; Car.prototype.drive = function() { console.log('Vrooom'); }; // Child "class" var Toyota = function() { Car.apply(this, arguments); }; Toyota.prototype = Car

How add new method in response and request

微笑、不失礼 提交于 2019-12-07 21:01:36
问题 I want to add new method in response and request of node.js. How i can do it more efficiently? I can't understand how this is done in express.js 回答1: Being JavaScript, there are numerous ways to do this. The pattern that seems most reasonable to me for express is to add the function to each request instance in an early middleware: //just an example function getBrowser() { return this.get('User-Agent'); } app.use(function (req, res, next) { req.getBrowser = getBrowser; next(); }); app.get('/',

Prototypal inheritance question in javascript

别来无恙 提交于 2019-12-07 15:50:11
问题 I understand what prototypal inheritance is all about, but I must be confused as to the implementation. I thought that modifying a function constructor's prototype would affect all instances of that constructor, but this isn't the case. How does JS do the method lookup from an object to its prototype? Here's an example function A(name){ this.name = name; } a = new A("brad"); A.prototype = { talk: function(){ return "hello " + this.name; } } a.talk() // doesn't work b = new A("john"); b.talk()