prototypal-inheritance

AngularJS - Which is better, $emit/$on or scope inheritance?

会有一股神秘感。 提交于 2019-12-07 05:17:10
问题 Say I got this following HTML structure: <body ng-app="demo" ng-controller="RootCtrl"> <header> <!-- Header Material --> </header> <main ng-controller="MainCtrl"> <!-- Main Content --> <nav ng-controller="NavCtrl"> <!-- Navbar --> </nav> </main> <body> Now, suppose NavCtrl needs to manipulate a model that happens to exist under RootCtrl 's scope - under which conditions would $emit/$on be better suited? And under which conditions would it be better to directly manipulate the model via scope

Preventing infinite recursion when using Backbone-style prototypal inheritance

放肆的年华 提交于 2019-12-07 03:20:58
问题 I'm using an extend function adapted from Backbone (identical apart from a few changes to comply with my employer's naming conventions) to implement prototypal inheritance. After setting up the following structure (much simplified below) I get an infinite loop. Graph = function () {}; Graph.extend = myExtendFunction; Graph.prototype = { generateScale: function () { //do stuff } } // base class defined elsewhere UsageGraph = Graph.extend({ generateScale: function () { this.constructor._super

How add new method in response and request

点点圈 提交于 2019-12-06 11:09:57
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 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('/', function (req, res) { //you can call req.getBrowser() here }); In express.js, this is done by adding

Backbone: annoying behaviour of prototype object

怎甘沉沦 提交于 2019-12-06 06:17:39
问题 I understand this is a problem (or behaviour) of javascript itself rather than Backbone's extend method, but I'd like to know what is the best strategy to avoid it. Let's better put it in code: var MyModel = Backbone.Model.extend({ value: 0, values: [] }); var myFirstModel = new MyModel(); myFirstModel.value // 0, as expected myFirstModel.values // [], as expected var mySecondModel = new MyModel(); mySecondModel.value = 2; mySecondModel.values.push(2) mySecondModel.value // 2, as expected

Prototypal inheritance question in javascript

半世苍凉 提交于 2019-12-05 23:05:10
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() // works I was under the impression that a would look for the method talk() in A 's prototype, so any

Fields are as static fields in Qooxdoo library

丶灬走出姿态 提交于 2019-12-05 22:01:08
I'd like to use qx-oo (Qooxdoo) as OOP library. But I was confused by strange behaviour of field members. It's looks like that fields are shared between all objects of one class, like static members. For example, this test code qx.Class.define("com.BaseClass", { extend : qx.core.Object, members: { _children: [], getChildrenCount: function(){ return this._children.length; }, addChild: function(child){ this._children.push(child); } } }); var class1 = new com.BaseClass(); var class2 = new com.BaseClass(); showLog("class1.getChildrenCount() - " + class1.getChildrenCount()) showLog("class2

AngularJS - Which is better, $emit/$on or scope inheritance?

泄露秘密 提交于 2019-12-05 11:24:15
Say I got this following HTML structure: <body ng-app="demo" ng-controller="RootCtrl"> <header> <!-- Header Material --> </header> <main ng-controller="MainCtrl"> <!-- Main Content --> <nav ng-controller="NavCtrl"> <!-- Navbar --> </nav> </main> <body> Now, suppose NavCtrl needs to manipulate a model that happens to exist under RootCtrl 's scope - under which conditions would $emit/$on be better suited? And under which conditions would it be better to directly manipulate the model via scope inheritance? If you're using prototypical inheritance, you need to be careful as it's easy to make

Preventing infinite recursion when using Backbone-style prototypal inheritance

吃可爱长大的小学妹 提交于 2019-12-05 07:04:32
I'm using an extend function adapted from Backbone (identical apart from a few changes to comply with my employer's naming conventions) to implement prototypal inheritance. After setting up the following structure (much simplified below) I get an infinite loop. Graph = function () {}; Graph.extend = myExtendFunction; Graph.prototype = { generateScale: function () { //do stuff } } // base class defined elsewhere UsageGraph = Graph.extend({ generateScale: function () { this.constructor._super.generateScale.call(this); // run the parent's method //do additional stuff } }) ExcessiveUsageGraph =

class use in a prototypal inheritance-based language

会有一股神秘感。 提交于 2019-12-05 02:13:38
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 there push to make the language something it is not. I'm looking for concrete examples of cases where

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

放肆的年华 提交于 2019-12-05 02:03:45
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({ defaults: { rabbit: 25 } }); var ei = new ExtendedInventory({}); console.log(ei.attributes); This outputs: