prototypal-inheritance

Javascript function using “this = ” gives “Invalid left-hand side in assignment”

▼魔方 西西 提交于 2019-11-28 13:32:56
I am trying to get a JavaScript object to use the "this" assignments of another objects' constructor, as well as assume all that objects' prototype functions. Here's an example of what I'm attempting to accomplish: /* The base - contains assignments to 'this', and prototype functions */ function ObjX(a,b) { this.$a = a; this.$b = b; } ObjX.prototype.getB() { return this.$b; } function ObjY(a,b,c) { // here's what I'm thinking should work: this = ObjX(a, b * 12); /* and by 'work' I mean ObjY should have the following properties: * ObjY.$a == a, ObjY.$b == b * 12, * and ObjY.getB == ObjX

What is the difference between using Object.create() and using assignment operator?

这一生的挚爱 提交于 2019-11-28 12:49:07
问题 Here are a few examples. // case 1: var obj1 = {msg : 'Hello'}; var obj2 = obj1; obj2.msg = "Hi!"; //overwrites alert(obj1.msg); //=>'Hi!' // case 2: var obj1 = {msg : 'Hello'}; var obj2 = Object.create(obj1); obj2.msg = "Hi!"; //does not overwrite alert(obj1.msg); //=>'Hello' // case 3: var obj1 = {data: { msg : 'Hello'}} var obj2 = Object.create(obj1); obj2.data.msg = "Hi!"; //overwrites, Why? alert(obj1.data.msg); //=>'Hi!' I think Object.create() just gives both makes both point to the

javascript class inherit from Function class

允我心安 提交于 2019-11-28 07:00:23
I like that in javascript, I can create a function, and then add further methods and attributes to that function myInstance = function() {return 5} myInstance.attr = 10 I would like to create a class to generate these objects. I assume I have to inherit from the Function base class. In other words, I would like to: var myInstance = new myFunctionClass() var x = myInstance() // x == 5 But I don't know how to create the myFunctionClass. I have tried the following, but it does not work: var myFunctionClass = function() {Function.call(this, "return 5")} myFunctionClass.prototype = new Function()

javascript inheritance from multiple objects

孤街醉人 提交于 2019-11-28 06:12:51
问题 I'm not very well aquainted with javascript inheritance, and I'm trying to make one object inherit from another, and define its own methods: function Foo() {} Foo.prototype = { getColor: function () {return this.color;}, }; function FooB() {} FooB.prototype = new Foo(); FooB.prototype = { /* other methods here */ }; var x = new FooB().getColor(); However, the second one overwrites the first one( FooB.prototype = new Foo() is cancelled out ). Is there any way to fix this problem, or am I going

How to create private variable accessible to Prototype function?

余生长醉 提交于 2019-11-28 04:35:09
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 = function(){ return ((typeof this.name !== 'undefined') ? this.name : 'Hybridicus Maximus'); }; Tree

Extending the defaults of a Model superclass in Backbone.js

非 Y 不嫁゛ 提交于 2019-11-27 20:28:08
问题 I would like to pose this as a question to this answer but I can't seem to do so, I apologize. Extending the defaults for the subclass are reflected in the superclass. This seems to defeat the purpose and I'm more apt to explicitly list the superclass' defaults in the subclass to get the structure I'm looking for. var Inventory = Backbone.Model.extend({ defaults: { cat: 3, dog: 5 } }); var ExtendedInventory = Inventory.extend({ }); _.extend(ExtendedInventory.prototype.defaults, {rabbit: 25});

Node.js / Express.js - How to override/intercept res.render function?

我怕爱的太早我们不能终老 提交于 2019-11-27 19:44:57
I'm building a Node.js app with Connect/Express.js and I want to intercept the res.render(view, option) function to run some code before forwarding it on to the original render function. app.get('/someUrl', function(req, res) { res.render = function(view, options, callback) { view = 'testViews/' + view; res.prototype.render(view, options, callback); }; res.render('index', { title: 'Hello world' }); }); It looks like a contrived example, but it does fit in an overall framework I'm building. My knowledge of OOP and Prototypal inheritance on JavaScript is a bit weak. How would I do something like

Setting prototype for Object Literal

邮差的信 提交于 2019-11-27 16:58:06
问题 Let's say I have the following code; var A = {a:10}; var B = {b:20}; B.prototype = A; alert(B.a); I am getting undefined for B.a . Am I doing something wrong? How do I set the prototype for object literal ? I know how to do for Constructor object. So the following code works perfect function A(){this.a=10} function B(){this.b=20} B.prototype = new A(); b = new B; alert(b.a); How do I do it for object literal ? 回答1: Objects inherit from their constructor's prototype property, not their own.

Angular - Changing scope is not getting reflected

血红的双手。 提交于 2019-11-27 16:24:18
This is weird as it should be pretty straightforward. I will post my code first and then ask the question: html - <div ng-controller="myController" ng-switch on="addressCards"> <div> {{addCustom}} // does not get changed <div ng-if="addCustom === false"> {{addCustom}} // does get changed <button type="button" class="btn btn-primary btn-icon-text" ng-click="addCustom = true"> <span class="icon icon-plus"></span> click here </button> </div> </div> </div> controller - (function(){ 'use strict'; angular.module('myApp') .controller('myController',['$scope',myController]); function myController(

Extending core types without modifying prototype

不打扰是莪最后的温柔 提交于 2019-11-27 13:37:34
How does one extend core JavaScript types (String, Date, etc.) without modifying their prototypes? For example, suppose I wanted to make a derived string class with some convenience methods: function MyString() { } MyString.prototype = new String(); MyString.prototype.reverse = function() { return this.split('').reverse().join(''); }; var s = new MyString("Foobar"); // Hmm, where do we use the argument? s.reverse(); // Chrome - TypeError: String.prototype.toString is not generic // Firefox - TypeError: String.prototype.toString called on incompatible Object The error seems to originate from