prototypal-inheritance

How do you take advantage of prototypal inheritance without needing to include files in a specific order?

When developing JavaScript, I tend to separate JavaScript code out into different files and then run a script to concatenate the files and compress or pack the resulting file. In the end, I have one file that I need to include on my production site. This approach has usually worked, but I've started to run into a problems with prototyp...

Javascript Prototypal Inheritance Doubt

Hi, I've been watching Douglas Crockford's talks at YUI Theater and I have a question about javascript inheritance... Douglas gives this example to show that "Hoozit" inherits from "Gizmo": function Hoozit(id) { this.id = id; } Hoozit.prototype = new Gizmo(); Hoozit.prototype.test = function (id) { return this.id === id; }; M...

Books on Prototypal Inheritance

My work in JavaScript and Ajax programming has stimulated a renewed interest in prototypal inheritance. Can anyone recommend any good books on the subject? In particular, books that deal with languages that predate JavaScript. This seems to be a very poorly documented subject matter, which is why I'm asking here. ...

Why to use class based OOP style inheritance in javascript?

If I'm not completely wrong every framework/library/approach in javascript tends today to mimick class based OOP style inheritance. Reasons for this seem to be people thinking class based OOP inheritance is far easier to understand and that most programmers know OOP. In my experience I don't find evidence for either of this opinions. I...

Javascript Augmenting basic types (prototype inheritance) doubt

I just started reading on Douglas Crockford's "Javascript The Good parts" where he explains about Augmenting basic types. Function.prototype.addMethod=function(name,func) { this.prototype[name]=func; return this; }; The moment after doing this, the addMethod becomes available for all basic objects like String, Number etc. Th...

Performing Inheritance in Javascript

Now while i know that you can not perform inheritance like you would in C# ,but i have seen mentions about it arround the net that it is kind of possible. If its not possible using plain javascript then would it be possible using Ext JS and if so how. ...

Good Example of JavaScript's Prototype-Based Inheritance

I have been programming with OOP languages for over 10 years but I'm learning JavaScript now and it's the first time I've encountered prototype-based inheritance. I tend to learn fastest by studying good code. What's a well-written example of a JavaScript application (or library) that properly uses prototypal inheritance? And can you des...

Why was JavaScript Implemented Using Prototypal Inheritance?

There's lots of articles and posts explaining how JavaScript inheritance works, but I'm curious why JavaScript was implemented using prototypal inheritance instead of classical inheritance. I love JavaScript so I'm not saying it's bad thing... I'm just curious. ...

Confused about JavaScript prototypal inheritance...

In the book "JavaScript the definitive guide 5 edition", section 9.2 Prototypes and Inheritance, I find the following words: In the previous section, I showed that the new operator creates a new, empty object and then invokes a constructor function as a method of that object. This is not the complete story, however. After c...

Prototypal inheritance in PHP (like in JavaScript)

Is it possible to employ some kind of prototypal inheritance in PHP like it is implemented in JavaScript? This question came to my mind just out of curiosity, not that I have to implement such thing and go against classical inheritance. It just feels like a interesting area to explore. Are there prebuild functions to combine classical ...

Javascript find beginning of prototype chain

If I set a function to Object.prototype, and try to call the function from object Foo, is there a way for the function to know what object originally called it? Object.prototype.MyFunc = function () { console.log("I was called by " + (//object name here...)); } Foo = {}; Foo.MyFunc(); Thanks! ...

How can I see a Javascript object's prototype chain?

Given the following code: function a() {} function b() {} b.prototype = new a(); var b1 = new b(); We can stay that a has been added to b's prototype chain. Great. And, all the following are true: b1 instanceof b b1 instanceof a b1 instanceof Object My question is, what if we don't know the origins of b1 ahead of time? How can we d...

Attaching methods to prototype from within constructor function

Here is the textbook standard way of describing a 'class' or constructor function in JavaScript, straight from the Definitive Guide to JavaScript: function Rectangle(w,h) { this.width = w; this.height = h; } Rectangle.prototype.area = function() { return this.width * this.height; }; I don't like the dangling prototype man...

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

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, ...

Javascript functional inheritance with prototypes

In Douglas Crockford's JavaScript: The Good Parts he recommends that we use functional inheritance. Here's an example: var mammal = function(spec, my) { var that = {}; my = my || {}; // Protected my.clearThroat = function() { return "Ahem"; }; that.getName = function() { return spec.name; ...

Calling function using 'new' is less expensive than without it?

Given this very familiar model of prototypal construction: function Rectangle(w,h) { this.width = w; this.height = h; } Rectangle.prototype.area = function() { return this.width * this.height; }; Can anyone explain why calling new Rectangle(2,3) is consistently 10x FASTER than calling Rectangle(2,3) without the 'new' keyw...

What is happening in Crockford's object creation technique?

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) 1) Object.create() starts out by creating an empty function called F. I'm thinking that a fu...

Does C# 4.0's ExpandoObject support Prototype-based inheritance?

Does C# 4.0's ExpandoObject support Prototype-based inheritance? If not, why not(was it by design?) and how could this be implemented? If yes, how does it work and differences are there to the way it works in Javascript? ...

Implementing prototypes OR instantiating class objects

updated 2010-06-15T09:45:00Z: added an example for the "classes as instances" approach, and an explanation of the "instances as classes" approach; incorporated and referenced Alex Martelli's answer; I'm wondering how to implement prototypal inheritance in Python. There would seem to be two different approaches to this problem: class...

Extjs: extend class via constructor or initComponent?

In extjs you can always extend an extjs class via the constructor(). For classes derinving from Component you can also extend via initComponent(). I am wondering why so many code extend via initComponent, whereas constructor seems to be the universal extension method. Does initComponent offer clear advantage over constructor? ...