JavaScript prototypal inheritance: succinct syntax?

Example function Name(first, last) { this. PartFirst = first; this. PartLast = last; this.

ValueOf = this. ToString = function() { return this. PartLast + ', ' + this.

PartFirst; } } Name. Prototype = new String().

Your method does not inherit its prototype from the String constructor any more. Side note: A valueOf method has to be defined, to prevent the following error: String.prototype. ValueOf called on incompatible Object (FF 8.0, using ""+(new Name()) as a test case).

– Rob W Dec 21 '11 at 21:11 noted, but explicitly calling .toString() as the OP intends to works fine. And wouldn't a parameter-less constructor call for Name be a programmer error? Updated link above to show (new Name("first", "last") + "") – jondavidjohn Dec 21 '11 at 21:14 new Name, new Name() and new Name(void 0, void 0) are equivalent and valid (void 0 === undefined, by the way).

As for the error, it occurs when using the OP's inheritance concept: jsfiddle.net/wKvqQ/4 – Rob W Dec 21 '11 at 21:23 @RobW found that the String.toString() uses the valueOf so all you have to do is define the valueOf. Check link above again. – jondavidjohn Dec 21 '11 at 21:25 That's not prototypical OO – Raynos Dec 21 '11 at 21:40.

Here's how I do it: function subclass(constructor, superConstructor) { function surrogateConstructor() { } surrogateConstructor. Prototype = superConstructor. Prototype; var prototypeObject = new surrogateConstructor(); prototypeObject.

Constructor = constructor; constructor. Prototype = prototypeObject; } /* Base object */ function BaseItem() { this. Type = 'baseitem'; this.

Obj = null; } BaseItem.prototype. Render = function() { return "foo"; } /* Sub class */ function InteractionArea() { BaseItem. Call(this); this.

Type = 'interactionarea' this. Obj = document. CreateElement('div') } subclass(InteractionArea, BaseItem); //here come the overrides InteractionArea.prototype.

Render = function() { return "foobar"; } InteractionArea.prototype. Render2 = function() { return "foobar"; } /* Sub-sub class */ function InteractionArea2() { InteractionArea. Call(this); this.

Type = 'interactionarea2'; this. Obj = false; } subclass(InteractionArea2, InteractionArea); InteractionArea2.prototype. Render = function() { return "bar"; }.

Sure. Use Object.create. Var Name = Object.

Create(String. Prototype, { toString: { value: function _toString() { return this. PartLast + ', ' + this.

PartFirst; } }, constructor: { value: function _constructor(first, last) { this. PartFirst = first; this. PartLast = last; return this; } } }); var name = Object.

Create(Name). Constructor("foo", "bar"); Now ES5 is a bit ugly, so you can use some mechanism for ES5 OO sugar, let's take pd as an example: var Name = pd. Make(String.

Prototype, { toString: function _toString() { return this. PartLast + ', ' + this. PartFirst; }, constructor: function (first, last) { this.

PartFirst = first; this. PartLast = last; }, beget: pd.Base. Beget }); console.

Log(Name. Beget("jimmy", "dean").toString()) console. Log(Name.

IsPrototypeOf(Name.beget())); console. Log(String.prototype. IsPrototypeOf(Name.beget())); console.

Log(Object.prototype. IsPrototypeOf(Name.beget())); console. Log(Name.toString.

Call(Name. Beget('jimmy', 'dean'))); console. Log(Name.toString.

Call({ partFirst: 'jimmy', partLast: 'dean' })); Of course output is as expected Live Example.

This is far uglier and verbose then the OP's own solution, not to mention the added need for a polyfill to account for any IE I never understood the need for Object. Create, it's a Crockfordism. – RobG Dec 22 '11 at 0:35 @RobG I prefer to not think in terms of constructors.

Constructors are ugly, all you need is a prototype object. The only thing that's ugly with the Object. Create solution is that instantiation and initialization is a two step thing – Raynos Dec 22 '11 at 12:13.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions