prototypal-inheritance

Inheritance in javascript, variables in the “parent”

て烟熏妆下的殇ゞ 提交于 2019-11-30 13:21:27
问题 I am doing OO javascript for the first time. I have read about inheritance and prototype and thought I had cracked it. Until I discovered this little example. function TestObject(data) { this.test_array = []; this.clone_array = []; this.dosomestuff = function() { for(var i=0; i<this.test_array.length; i++) { this.clone_array[i]=this.test_array[i]; } } this.__construct = function(data) { console.log("Testing Object Values" ,this.clone_array); this.test_array = data; }; } TestObject2.prototype

JavaScript Inheritance with Prototypes — 'constructor' property?

会有一股神秘感。 提交于 2019-11-30 12:49:57
问题 I've seen a lot of stuff like this, and am looking for the proper solution to basic JavaScript inheritance: function Food(){} // Food constructor (class) function Bread(){} // Bread constructor (class) var basicFood = new Food(); // Food classes will inherit from this basicFood instance. Bread.prototype = basicFood; // Bread now inherits from Food. var bread = new Bread(); // We create some bread! bread.constructor == Food; // Though, now we feel very uneasy about how // the constructor is

Can JavaScript constructor return function and keep inheritance?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 11:33:28
function F() { return function() { return {}; } } var f = new F(); f instanceof F; // returns false As far as I understand, if I want instanceof to work, I need to return this from the constructor. But I want the constructor to return a function, and I cannot assign to this . So, is this really impossible or can it be done somehow, for f = new F() to return a function and still f instanceof F to return true? function F() { var r = function() { return {}; }; r.__proto__ = this.__proto__; return r; } var f = new F(); f instanceof F; true f(); Object Only works in the browsers with __proto__ You

Mixing Google Maps custom overlays with Backbone Views

夙愿已清 提交于 2019-11-30 09:47:36
TL;DR Is PinView.prototype = _.extend(PinView.prototype, google.maps.OverlayView.prototype) the "proper" way to have a Backbone View inherit from another "class"? Long read We're redoing our site using Backbone and are working on including some mapping functionality. I've got a Backbone view that handles placing <div> s onto specific points within the browser window; this seems like a natural thing to extend in order have Google's Map API place them on geographical coordinates. According to the Google API , in order to generate a custom overlay you create a new object and set the prototype for

Inheritance in javascript, variables in the “parent”

梦想的初衷 提交于 2019-11-30 07:11:48
I am doing OO javascript for the first time. I have read about inheritance and prototype and thought I had cracked it. Until I discovered this little example. function TestObject(data) { this.test_array = []; this.clone_array = []; this.dosomestuff = function() { for(var i=0; i<this.test_array.length; i++) { this.clone_array[i]=this.test_array[i]; } } this.__construct = function(data) { console.log("Testing Object Values" ,this.clone_array); this.test_array = data; }; } TestObject2.prototype = new TestObject(); function TestObject2(data) { this.__construct(data); this.dothings = function() {

EXTENDS challenge: preprocessor function macros and class-like oop

吃可爱长大的小学妹 提交于 2019-11-30 05:34:36
Background I've been using the C preprocessor to manage and "compile" semi-large javascript projects with multiple files and build targets. This gives full access to C preprocessor directives like #include , #define , #ifdef , etc. from within javascript. Here's a sample build script so you can test the example code: #!/bin/bash export OPTS="-DDEBUG_MODE=1 -Isrc" for FILE in `find src/ | egrep '\.js?$'` do echo "Processing $FILE" cat $FILE \ | sed 's/^\s*\/\/#/#/' \ | cpp $OPTS \ | sed 's/^[#:<].*// ; /^$/d' \ > build/`basename $FILE`; done Make a src and a build directory, and put the .js

Minor drawback with Crockford Prototypical Inheritance

风流意气都作罢 提交于 2019-11-30 04:29:27
问题 Just experimenting with different inheritance techniques in JS, and came across something mildly discomfiting about Crockford's Prototypal Inheritance pattern: function object(o) { function F() {} F.prototype = o; return new F(); } var C, P = { foo:'bar', baz: function(){ alert("bang"); } } C = object(P); It's all good - except when you log to console - the object appears as F. I've seen classical emulation in which you can repoint the constructor - is there a similar method to coerce the

JavaScript Inheritance with Prototypes — 'constructor' property?

▼魔方 西西 提交于 2019-11-30 03:17:34
I've seen a lot of stuff like this, and am looking for the proper solution to basic JavaScript inheritance: function Food(){} // Food constructor (class) function Bread(){} // Bread constructor (class) var basicFood = new Food(); // Food classes will inherit from this basicFood instance. Bread.prototype = basicFood; // Bread now inherits from Food. var bread = new Bread(); // We create some bread! bread.constructor == Food; // Though, now we feel very uneasy about how // the constructor is wrong, Bread.prototype.constructor = Bread; // So we explicitly set the prototype's constructor bread =

Implement multiple inheritance in Javascript

回眸只為那壹抹淺笑 提交于 2019-11-29 23:19:25
问题 I have observed that through the use of "prototype" property in Javascript (and indirectly setting up the prototype chain), one can implement multi-level inheritance in JavaScript. But is it possible to implement multiple inheritance in Javascript by some way? Any simple examples would be great. 回答1: To implement simple inheritance you usually do MyClass.prototype = new MySuperClass(); but you could also copy the content of another "class" : MyClass.prototype = new MySuperClass(); var

(Open Source) Examples of JavaScript Prototypical OO

白昼怎懂夜的黑 提交于 2019-11-29 19:27:21
Bounty Edit: I'm looking for code written in a pure prototypical OO paradigm (think Self). Not a mixture of prototypical OO and classical OO. I don't want to see generic OO wrappers but simply usage of prototypical OO techniques and only prototypical OO techniques. Reference Related Question: Prototypical OO in JavaScript In the above question I mainly focused on Can write prototypical OO like this? Do we need constructors and initialization logic, What are the alternatives? New question: Basically are there any good examples of javascript prototypical OO in large open source projects?