prototypal-inheritance

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

自闭症网瘾萝莉.ら 提交于 2019-11-29 17:27:42
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 same prototype, while assignment makes both object point to same location(not just prototype). But then

Mixing Google Maps custom overlays with Backbone Views

心不动则不痛 提交于 2019-11-29 15:23:33
问题 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

Object.create Prototype Chains

六眼飞鱼酱① 提交于 2019-11-29 15:13:59
Initial Question Yesterday i read about ECMAScript 5 Object.create() And I wanted to start building prototype Chains in my Code with this method instead of setting the prototype and its constructor, I like that you can directly set writable configurable etc.. I tried it like this function printobject(msg, obj) { if (msg) { document.write("<b>" + msg + "</b><br>"); document.write("<hr><br>"); } for (var prop in obj) { if (obj.hasOwnProperty(prop)) { if (obj[prop].toString() !== "[object Object]") { document.write(prop + " : " + obj[prop] + "<br>"); } else { document.write("<b>" + prop + " : " +

javascript inheritance from multiple objects

蹲街弑〆低调 提交于 2019-11-29 12:45:21
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 in the wrong direction? Thanks in advance, sorry for any bad terminology. Each object can only have

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

落爺英雄遲暮 提交于 2019-11-29 12:21:59
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.EventEmitter.prototype; The author of the article did not just use this line of code: Door.prototype=

Setting prototype for Object Literal

给你一囗甜甜゛ 提交于 2019-11-29 05:20:33
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 ? Objects inherit from their constructor's prototype property, not their own. The constructor's prototype is assigned to the internal [[Prototype]] property that is available in some

EXTENDS challenge: preprocessor function macros and class-like oop

感情迁移 提交于 2019-11-29 03:27:12
问题 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/^[#

prototypal inheritance concept in javascript as a prototype based language

☆樱花仙子☆ 提交于 2019-11-28 19:52:43
You know Javascript is a prototype-based programming language . I have read some books about Javascript and its prototypal inheritance concept but: "If you can't explain it to a six-year old, you really don't understand it yourself.”. Well, I tried to explain JavaScript prototype concept to a 22-year old friend and completely failed! How would you explain it to a 6-year old person that is strangely interested in that subject? I have seen some examples given in Stack Overflow, and it did not help. Classical inheritance is about extending types of things. Say you have a class, like Bike . When

What is happening in Crockford's object creation technique?

柔情痞子 提交于 2019-11-28 16:53:06
There are only 3 lines of code, and yet I'm having trouble fully grasping this: Object.create = function (o) { function F() {} F.prototype = o; return new F(); }; newObject = Object.create(oldObject); (from Prototypal Inheritance ) Object.create() starts out by creating an empty function called F . I'm thinking that a function is a kind of object. Where is this F object being stored? Globally I guess. Next our oldObject , passed in as o , becomes the prototype of function F . Function (i.e., object) F now "inherits" from our oldObject , in the sense that name resolution will route through it.

Prototypical OO in JavaScript

拟墨画扇 提交于 2019-11-28 15:18:06
TL;DR: Do we need factories/constructors in prototypical OO? Can we make a paradigm switch and drop them completely? The BackStory: I've been toying with doing prototypical OO in JavaScript lately and find that 99% of OO done in JavaScript is forcing classical OO patterns into it. My take on prototypical OO is that it involves two things. A static prototype of methods (and static data) and a data binding. We don't need factories or constructors. In JavaScript these are Object literals containing functions and Object.create . This would mean we can model everything as a static blueprint