qwertypants: > I'm a bit new to Object Inheritance in JavaScript and I'd like to find > the best way to do so. I have seen a few techniques from Resig to > Crockford, but I'd like to know the simplest and most effective way to > create object from existing ones. Is there any native way that this > can be done elegantly? Am I going to have to create a function to > create my objects using something like Crockford's beget method?
I think you first should read what is the architecture of objects and inheritance in Javascript. <URL: https://developer.mozilla.org/en/JavaScript/Guide/Inheritance_Revisited> <URL: http://dmitrysoshnikov.com/ecmascript/chapter-7-1-oop-general-theory/> <URL: http://dmitrysoshnikov.com/ecmascript/chapter-7-2-oop-ecmascript-implementation/> After reading these, you will be able to decide which approach is better according your requirements. > For example, I'm using Raphael.js to construct a circle Object that > I'd like other Objects to inherit from: > > var Circle = function(){ > return { > type: "circle", > x: 0, > y: 0, > cx: 31, > cy: 32, > r: 1, > fill: "#00AEEF" > } > } > To get a new instance of it, I'm using this: > var inner = new Circle; At all you don't need to use `new` keyword here, unless you want to create new native object by the internal [[Construct]] method of `Circle'. The worst part of this design is that on every instance you are going to allocate memory for whole interface of this object. Trough the prototype chain is definitely better to be done this. For example: function F() { } F.prototype = { constructor : F, foo : function () { }, bar : function () { } }; var instance1 = new F, instance2 = new F; instance1.foo(); instance2.foo(); instance1.foo === F.prototype.foo; //true instance2.foo === F.prototype.foo; //true So both instance inherit properties trough the their prototype chain from `F.prototype'. The prototype chain for example of `instance1` is: instance1 -> F.prototype -> Object.prototype -> null The prototype chain is a list of objects. This list of object is formed by internal property [[Prototype]] which every native object has it. While this property is internal you are not able to access it or overwrite its value. Some implementations provide non-standard property `__proto__` which is exactly mimic of internal [[Prototype]]. For example: instance1.__proto__ === F.prototype; //true instance1.__proto__.__proto__ === Object.prototype; //true instance1.__proto__.__proto__.__proto__ === null; //true In normal case the end of prototype chain is always `Object.prototype`. Its [[Prototype]] value is `null' which marks the end of the prototype chain. It is important for resolving properties against the prototype chain. Another problem of presented approach from you is `instanceof'. inner instanceof Circle; //false While `inner' has not reference to `Circle.prototype' in its prototype chain, `instanceof` will return `false`. Read the resources which I gave you, there is enough information. -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
