This link has been bookmarked by 189 people . It was first bookmarked on 02 Mar 2006, by Jeremy Wei.
-
31 Dec 17
-
06 Sep 16
-
14 Jun 16
-
JavaScript's prototypal inheritance has more expressive power than classical inheritance, as we will see presently.
-
When I write a method that doesn't need to return a value, I usually have it return this. It allows for a cascade-style of programming.
-
Deep hierarchies are inappropriate. Shallow hierarchies are efficient and expressive.
-
-
22 Mar 16
-
uses prototypal inheritance instead of classical inheritance.
-
inherits method is similar to Java's extends
-
uber method is similar to Java's super
-
-
28 Oct 15
-
13 Oct 15
-
Because objects in JavaScript are so flexible, you will want to think differently about class hierarchies. Deep hierarchies are inappropriate. Shallow hierarchies are efficient and expressive.
-
I now see my early attempts to support the classical model in JavaScript as a mistake.
-
-
07 Sep 15
-
10 Apr 15
-
Multiple Inheritance
-
By manipulating a function's prototype object, we can implement multiple inheritance, allowing us to make a class built from the methods of multiple classes. Promiscuous multiple inheritance can be difficult to implement and can potentially suffer from method name collisions. We could implement promiscuous multiple inheritance in JavaScript, but for this example we will use a more disciplined form called Swiss Inheritance.
-
-
15 Dec 14
-
14 Aug 14
-
15 Jul 14
-
Classical Inheritance in JavaScript
-
The first is type convenience
-
The second reason is code reuse
-
It takes a name and a function, and adds them to a function's prototype object.
-
-
03 Jun 14
-
25 Feb 14
-
11 Feb 14
-
31 Dec 13
-
28 Dec 13
-
02 Dec 13
carlos puentes"Classical Inheritance in JavaScript"
-
12 Nov 13
-
13 Apr 13
-
primarily two reasons
-
type convenience
-
cast references of similar classes. Little type-safety is obtained from a type system which requires the routine explicit casting of object references. This is of critical importance in strongly-typed languages, but it is irrelevant in loosely-typed languages like JavaScript, where object references never need casting
-
-
13 Feb 13
-
07 Jan 13
-
08 Dec 12
-
I have been writing JavaScript for 8 years now, and I have never once found need to use an
uberfunction. The super idea is fairly important in the classical pattern, but it appears to be unnecessary in the prototypal and functional patterns. I now see my early attempts to support the classical model in JavaScript as a mistake.
-
-
09 Nov 12
-
05 Nov 12
-
04 Oct 12
-
21 Sep 12
-
12 Sep 12
-
24 Jun 12
-
16 Apr 12
-
11 Apr 12
-
13 Mar 12
-
09 Mar 12
-
19 Feb 12
-
JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance.
-
The first is type convenience.
-
why do we care about inheritance at all?
-
The second reason is code reuse
-
Parasitic Inheritance
-
Classical inheritance is about the is-a relationship, and parasitic inheritance is about the was-a-but-now's-a relationship. The constructor has a larger role in the construction of the object.
-
JavaScript's dynamism allows us to add or replace methods of an existing class
-
the method method at any time, and all present and future instances of the class will have that method
-
Class Augmentation
-
Recall that JavaScript objects are like hashtables. You can add new values at any time. If the value is a function, then it becomes a method.
-
myParenizor = new Parenizor(0);
-
myParenizor.toString = function () {
-
When I write a method that doesn't need to return a value, I usually have it return this.
-
Classical objects are hard. The only way to add a new member to a hard object is to create a new class. In JavaScript, objects are soft. A new member can be added to a soft object by simple assignment.
-
I have never once found need to use an
uberfunction. The super idea is fairly important in the classical pattern, but it appears to be unnecessary in the prototypal and functional patterns.
-
-
08 Feb 12
-
25 Jan 12
-
08 Dec 11
-
04 Nov 11
-
15 Oct 11
-
11 Oct 11
-
19 Sep 11
-
30 Aug 11
-
04 Aug 11
-
can implement multiple inheritance
-
-
03 Aug 11
-
25 Feb 11
-
07 Feb 11
-
07 Jan 11
-
22 Nov 10
-
16 Nov 10
-
11 Aug 10
-
JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance. This can be puzzling to programmers trained in conventional object-oriented languages like C++ and Java. JavaScript's prototypal inheritance has more expressive power than classical inheritance, as we will see presently.
Java JavaScript Strongly-typed Loosely-typed Static Dynamic Classical Prototypal Classes Functions Constructors Functions Methods Functions But first, why do we care about inheritance at all? There are primarily two reasons. The first is type convenience. We want the language system to automatically cast references of similar classes. Little type-safety is obtained from a type system which requires the routine explicit casting of object references. This is of critical importance in strongly-typed languages, but it is irrelevant in loosely-typed languages like JavaScript, where object references never need casting.
The second reason is code reuse. It is very common to have a quantity of objects all implementing exactly the same methods. Classes make it possible to create them all from a single set of definitions. It is also common to have objects that are similar to some other objects, but differing only in the addition or modification of a small number of methods. Classical inheritance is useful for this but prototypal inheritance is even more useful.
To demonstrate this, we will introduce a little sugar which will let us write in a style that resembles a conventional classical language. We will then show useful patterns which are not available in classical languages. Then finally, we will explain the sugar.
-
JavaScript does not have classes, but we can program as though it does.
-
-
12 Jul 10
-
We want the language system to automatically cast references of similar classes.
-
code reuse.
-
The uber method is similar to Java's super. It lets a method call a method of the parent class.
-
-
21 Jun 10
-
08 May 10
-
06 May 10
-
09 Feb 10
-
21 Jan 10
-
05 Dec 09
-
18 Oct 09
-
11 Oct 09
-
21 Sep 09
-
24 Mar 09
-
04 Feb 09
-
10 Jan 09
-
31 Dec 08
-
14 Nov 08
-
20 Oct 08
-
12 Sep 08
-
11 Sep 08
-
03 Aug 08
-
21 Jun 08
-
Classical Inheritance
-
Multiple Inheritance
-
Function.prototype.method = function (name, func) { this.prototype[name] = func; return this; };
-
Function.method('inherits', function (parent) { var d = {}, p = (this.prototype = new parent()); this.method('uber', function uber(name) { if (!(name in d)) { d[name] = 0; } var f, r, t = d[name], v = parent.prototype; if (t) { while (t) { v = v.constructor.prototype; t -= 1; } f = v[name]; } else { f = p[name]; if (f == this[name]) { f = v[name]; } } d[name] += 1; r = f.apply(this, Array.prototype.slice.apply(arguments, [1])); d[name] -= 1; return r; }); return this; });
-
Function.method('swiss', function (parent) { for (var i = 1; i < arguments.length; i += 1) { var name = arguments[i]; this.prototype[name] = parent.prototype[name]; } return this; });
-
-
Benx ShenJavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance. This can be puzzling to programmers trained in conventional object-oriented languages like C++ and Java. JavaScript's prototypal inheritance has more expressive power than classical inheritance, as we will see presently.
-
08 May 08
-
26 Mar 08
-
24 Mar 08
-
21 Mar 08
-
23 Feb 08
-
16 Feb 08
-
04 Jan 08
-
07 Dec 07
-
04 Dec 07
-
23 Nov 07
-
16 Nov 07
-
15 Aug 07
-
22 Jun 07
-
04 May 07
-
20 Feb 07
-
20 Jan 07
-
06 Dec 06
-
03 Nov 06
-
30 Oct 06
-
26 Oct 06
-
02 Oct 06
-
19 Sep 06
-
18 Sep 06
-
23 Aug 06
-
16 Aug 06
-
10 Aug 06
-
04 Aug 06
Page Comments
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.