This link has been bookmarked by 103 people . It was first bookmarked on 29 Dec 2006, by Jonathan Aquino.
-
05 Sep 16
-
prototypal inheritance
-
The first is type convenience.
-
Loosely-typed
-
Dynamic
-
Prototypal
-
Functions
-
Functions
-
The second reason is code reuse.
-
Functions
-
In JavaScript, objects are soft. A new member can be added to a soft object by simple assignment.
-
Shallow hierarchies are efficient and expressive.
-
-
14 Jul 16
-
06 Jul 16
-
29 Apr 16
shiroimo呃。。大致得看了下前面。。。。。是 stackoverflow 上一个问题的最高票回答。。但是我觉得相关性不大是怎么回事qwq。。。 http://stackoverflow.com/questions/2064731/good-example-of-javascripts-prototype-based-inheritance
-
why do we care about inheritance at all? There are primarily two reasons. The first is type convenience.
-
The second reason is code reuse.
-
The method method takes a method name and a function, adding them to the class as a public method.
-
The inherits method is similar to Java's extends.
-
The uber method is similar to Java's super. It lets a method call a method of the parent class.
-
Promiscuous multiple inheritance can be difficult to implement and can potentially suffer from method name collisions
-
a more disciplined form
-
Parasitic Inheritance
-
Classical inheritance is about the is-a relationship, and parasitic inheritance is about the was-a-but-now's-a relationship.
-
Class Augmentation
-
-
06 Dec 15
media geeza"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."
-
28 Sep 15
-
10 Aug 15
-
14 Jan 15
-
Classical Inheritance in JavaScript
-
And you think you're so clever and classless and free
— John Lennon
-
JavaScript is a class-free, object-oriented language
-
it uses prototypal inheritance instead of classical inheritance.
-
JavaScript's prototypal inheritance has more expressive power than classical inheritance, as we will see presently.
-
The first is type convenience
-
There are primarily two reasons
-
But first, why do we care about inheritance at all?
-
Java JavaScript Strongly-typed Loosely-typed Static Dynamic Classical Prototypal Classes Functions Constructors Functions Methods Functions -
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,
-
where object references never need casting.
-
but it is irrelevant in loosely-typed languages like JavaScript
-
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.
-
Classical Inheritance
-
First, we will make a Parenizor class that will have set and get methods for its value, and a toString method that will wrap the value in parens.
-
function Parenizor(value) { this.setValue(value); }
-
Parenizor.method('setValue', function (value) { this.value = value; return this; });
-
Parenizor.method('getValue', function () { return this.value; });
-
Parenizor.method('toString', function () { return '(' + this.getValue() + ')'; });
-
The syntax is a little unusual, but it is easy to recognize the classical pattern in it. The method method takes a method name and a function, adding them to the class as a public method.
-
So now we can write
-
myParenizor = new Parenizor(0);
-
myString = myParenizor.toString();
-
As you would expect, myString is "(0)".
-
Now we will make another class which will inherit from Parenizor, which is the same except that its toString method will produce "-0-" if the value is zero or empty.
-
JavaScript does not have classes, but we can program as though it does.
-
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
-
We could implement promiscuous multiple inheritance in JavaScript, but for this example we will use a more disciplined form called Swiss Inheritance.
-
Parasitic Inheritance
-
Class Augmentation
-
We can call the method method at any time, and all present and future instances of the class will have that method. We can literally extend a class at any time. Inheritance works retroactively. We call this Class Augmentation to avoid confusion with Java's extends, which means something else.
-
JavaScript's dynamism allows us to add or replace methods of an existing class.
-
Object Augmentation
-
In the static object-oriented languages, if you want an object which is slightly different than another object, you need to define a new class
-
This has enormous power because you can write far fewer classes and the classes you do write can be much simpler.
-
In JavaScript, you can add methods to individual objects without the need for additional classes.
-
ecall that JavaScript objects are like hashtables.
-
ou can add new values at any time
-
If the value is a function, then it becomes a method.
-
Sugar
-
o make the examples above work, I wrote four sugar methods. First, the method method, which adds an instance method to a class.
-
Function.prototype.method = function (name, func) { this.prototype[name] = func; return this; };
-
This adds a public method to the Function.prototype, so all functions get it by Class Augmentatio
-
t takes a name and a function, and adds them to a function's prototype object.
-
It returns this. 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.
-
Next comes the inherits method, which indicates that one class inherits from another. It should be called after both classes are defined, but before the inheriting class's methods are added.
-
Conclusion
-
JavaScript can be used like a classical language, but it also has a level of expressiveness which is quite unique.
-
We have looked at Classical Inheritance, Swiss Inheritance, Parasitic Inheritance, Class Augmentation, and Object Augmentation.
-
his large set of code reuse patterns comes from a language which is considered smaller and simpler than Java.
-
In JavaScript, objects are soft. A new member can be added to a soft object by simple assignment.
-
Classical objects are hard.
-
The only way to add a new member to a hard object is to create a new class
-
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.
-
The super idea is fairly important in the classical pattern, but it appears to be unnecessary in the prototypal and functional patterns
-
I have been writing JavaScript for 8 years now, and I have never once found need to use an
uberfunction. -
I now see my early attempts to support the classical model in JavaScript as a mistake.
-
-
15 Oct 14
-
13 Oct 14
-
Class Augmentation to avoid confusion with Java's extends, which means something else.
-
can be used like a classical language, but it also has a level of expressiveness which is quite unique
-
A new member can be added to a soft object by simple assignment.
-
you will want to think differently about class hierarchies.
-
Deep hierarchies are inappropriate. Shallow hierarchies are efficient and expressive.
-
appears to be unnecessary in the prototypal and functional patterns
-
-
01 Sep 14
-
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.
-
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.
-
Notice that the uber née super method is still available to the privileged methods.
-
JavaScript's dynamism allows us to add or replace methods of an existing class. We can call the method method at any time, and all present and future instances of the class will have that method. We can literally extend a class at any time. Inheritance works retroactively. We call this Class Augmentation to avoid confusion with Java's extends, which means something else.
-
In the static object-oriented languages, if you want an object which is slightly different than another object, you need to define a new class. In JavaScript, you can add methods to individual objects without the need for additional classes.
-
This adds a public method to the Function.prototype, so all functions get it by Class Augmentation.
-
It returns this. When I write a method that doesn't need to return a value, I usually have it return this.
-
Array.prototype.slice.apply(arguments, [1])
-
Deep hierarchies are inappropriate. Shallow hierarchies are efficient and expressive.
-
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.
-
-
17 Jul 14
-
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.
-
The first is type convenience.
-
The second reason is code reuse.
-
Classical inheritance is useful for this but prototypal inheritance is even more useful.
-
JavaScript can be used like a classical language, but it also has a level of expressiveness which is quite unique. We have looked at Classical Inheritance, Swiss Inheritance, Parasitic Inheritance, Class Augmentation, and Object Augmentation. This large set of code reuse patterns comes from a language which is considered smaller and simpler than Java
-
-
03 Apr 14
-
29 Jan 13
-
30 Oct 12
-
05 Sep 12
-
09 May 12
-
06 Apr 12
-
I now see my early attempts to support the classical model in JavaScript as a mistake.
-
-
06 Mar 12
-
17 Feb 12
-
07 Dec 11
-
14 Nov 11
-
16 Jul 11
-
this.prototype[name] = func;
-
It returns this. 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.
-
-
10 Jun 11
-
29 Mar 11
-
Classical Inheritance
-
Sugar
-
-
17 Mar 11
-
15 Mar 11
-
22 Feb 11
-
06 Feb 11
-
08 Dec 10
-
04 Nov 10
-
11 Oct 10
-
31 Aug 10
-
22 Aug 10
-
08 May 10
-
12 Feb 10
-
JavaScript's prototypal inheritance has more expressive power than classical inheritance, as we will see presently.
-
function ZParenizor(value) { this.setValue(value); } ZParenizor.inherits(Parenizor); ZParenizor.method('toString', function () { if (this.getValue()) { return this.uber('toString'); } return "-0-"; });
-
-
03 Feb 10
-
09 Jan 10
Simone Economo"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 prot
classical inheritance object-oriented objects oo javascript programming development bestpractices js prototype prototype-oriented oop webdev
-
17 Nov 09
-
24 Sep 09
-
18 Aug 09
-
08 Aug 09
-
23 Jul 09
-
15 Jun 09
-
30 May 09
-
20 Mar 09
Attila GyörffyJavaScript 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 proto
-
04 Mar 09
-
11 Feb 09
-
01 Feb 09
-
23 Jan 09
-
27 Nov 08
-
06 Nov 08
max jJavaScript can be used like a classical language, but it also has a level of expressiveness which is quite unique. We have looked at Classical Inheritance, Swiss Inheritance, Parasitic Inheritance, Class Augmentation, and Object Augmentation. This large s
-
13 Oct 08
-
17 Sep 08
-
25 Aug 08
-
26 Jul 08
-
26 Jun 08
-
06 Jun 08
-
21 Apr 08
-
02 Feb 08
-
04 Jan 08
-
03 Jan 08
-
23 Oct 07
-
05 Oct 07
-
JavaScript's dynamism allows us to add or replace methods of an existing class. We can call the method method at any time, and all present and future instances of the class will have that method.
-
In the static object-oriented languages, if you want an object which is slightly different than another object, you need to define a new class. In JavaScript, you can add methods to individual objects without the need for additional classes. This has enormous power because you can write far fewer classes and the classes > you do write can be much simpler >
-
This has enormous power because you can write far fewer classes and the classes you do write can be much simpler.
-
-
05 Sep 07
-
03 Jul 07
-
19 Mar 07
-
29 Dec 06
-
It returns this. 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.
-
-
22 Aug 06
-
21 Jul 06
-
26 May 06
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.