This link has been bookmarked by 123 people . It was first bookmarked on 20 Dec 2006, by eliazar.
-
04 Sep 17
-
06 Sep 16
-
In a prototypal system, objects inherit from objects.
-
-
06 Jul 16
-
14 Jan 16
-
function object(o) { function F() {} F.prototype = o; return new F(); }
-
So instead of creating classes, you make prototype objects, and then use the
objectfunction to make new instances. -
augment the new instances, giving them new fields and methods. These can then act as prototypes for even newer objects.
-
-
14 Oct 15
-
I have found that by using these tools, coupled with JavaScript's lambdas and object quasi-literals, I can write well-structured programs that are large, complex, and efficient. The classical object model is by far the most popular today, but I think that the prototypal object model is more capable and offers more expressive power.
-
-
10 Aug 15
-
27 Mar 15
-
14 Jan 15
-
Prototypal Inheritance in JavaScript
-
It showed that JavaScript is a class-free,
-
Five years ago I wrote Classical Inheritance in JavaScript
-
and that it has sufficient expressive power to simulate a classical system.
-
prototypal language,
-
I have learned to fully embrace prototypalism, and have liberated myself from the confines of the classical model.
-
In a prototypal system, objects inherit from objects. JavaScript, however, lacks an operator that performs that operation
-
My journey was circuitous because JavaScript itself is conflicted about its prototypal nature
-
nstead it has a
newoperator, such thatnewf() -
produces a new object that inherits from
-
f
.prototype -
This indirection was intended to make the language seem more familiar to classically trained programmers,
-
It is a standard feature in my toolkit, and I highly recommend it for yours.
-
Fortunately, it is easy to create an operator that implements true prototypal inheritance.
-
function object(o) { function F() {} F.prototype = o; return new F(); }
-
It takes an old object as a parameter and returns an empty new object that inherits from the old one.
-
The
objectfunction untangles JavaScript's constructor pattern, achieving true prototypal inheritance -
If we attempt to obtain a member from the new object, and it lacks that key, then the old object will supply the member. Objects inherit from objects. What could be more object oriented than that?
-
Objects are mutable in JavaScript, so we can augment the new instances, giving them new fields and methods.
-
So instead of creating classes, you make prototype objects, and then use the
objectfunction to make new instances -
These can then act as prototypes for even newer objects
-
We don't need classes to make lots of similar objects.
-
For convenience, we can create functions which will call the
objectfunction for us, and provide other customizations such as augmenting the new objects with privileged functions -
I sometimes call these maker functions. If we have a maker function that calls another maker function instead of calling the
objectfunction, then we have a parasitic inheritance pattern. -
I can write well-structured programs that are large, complex, and efficient.
-
I have found that by using these tools, coupled with JavaScript's lambdas and object quasi-literals
-
The classical object model is by far the most popular today, but I think that the prototypal object model is more capable and offers more expressive power.
-
Learning these new patterns also made me a better classical programmer. Insights from the dynamic world can have application in the static.
-
Here is another formulation:
-
Object.prototype.begetObject = function () { function F() {} F.prototype = this; return new F(); }; newObject = oldObject.begetObject();
-
The problem with the
objectfunction is that it is global, and globals are clearly problematic. The problem withObject.prototype.begetObjectis that it trips up incompetent programs, and it can produce unexpected results whenbegetObjectis overridden. -
So I now prefer this formulation:
-
if (typeof Object.create !== 'function') { Object.create = function (o) { function F() {} F.prototype = o; return new F(); }; } newObject = Object.create(oldObject);
-
-
30 Sep 14
-
23 Sep 14
-
Five years ago I wrote Classical Inheritance in JavaScript
-
I sometimes call these maker functions. If we have a maker function that calls another maker function instead of calling the
objectfunction, then we have a parasitic inheritance pattern. -
privileged functions
-
JavaScript's lambdas and object quasi-literals
-
-
19 May 14
-
03 Apr 14
-
28 Feb 14
-
28 Dec 13
-
12 Nov 13
-
29 Sep 13
-
13 Aug 13
-
21 Apr 13
-
09 Apr 13
-
21 Mar 13
-
02 Mar 13
-
17 Jan 13
-
05 Sep 12
-
29 May 12
-
09 May 12
-
15 Feb 12
-
13 Feb 12
-
15 Jan 12
-
05 Jan 12
-
07 Dec 11
-
29 Nov 11
-
if (typeof Object.create !== 'function') { Object.create = function (o) { function F() {} F.prototype = o; return new F(); }; } newObject = Object.create(oldObject);
-
-
21 Nov 11
-
15 Oct 11
-
08 Sep 11
-
17 Mar 11
-
15 Mar 11
-
14 Feb 11
-
10 Feb 11
-
06 Feb 11
-
19 Jan 11
-
30 Dec 10
-
23 Dec 10
-
21 Jul 10
-
was circuitous because Jav
-
-
23 May 10
Michael JoyceFive years ago I wrote Classical Inheritance in JavaScript. It showed that JavaScript is a class-free, prototypal language, and that it has sufficient expressive power to simulate a classical system. My programming style has evolved since then, as any goo
ajax oop patterns js javascript prototype inheritance webdev object-oriented fromDelicious
-
22 May 10
-
11 Mar 10
-
09 Jan 10
Simone Economo"Five years ago I wrote Classical Inheritance in JavaScript. It showed that JavaScript is a class-free, prototypal language, and that it has sufficient expressive power to simulate a classical system. My programming style has evolved since then, as any go
javascript prototype prototypal inheritance object-oriented prototype-oriented programming development language oop webdev object
-
24 Sep 09
-
06 Sep 09
-
28 Mar 09
-
03 Mar 09
-
19 Feb 09
-
23 Jan 09
Rob CampbellCrockford renounces his earlier attempt to make javascript behave like a classical language
-
22 Sep 08
-
05 Sep 08
-
25 Aug 08
-
24 Jan 08
-
05 Sep 07
-
19 May 07
-
02 Apr 07
-
07 Feb 07
-
20 Dec 06
-
15 Dec 06
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.