JavaScript Inheritance
function Parenizor (value) {
this. SetValue (value); .

} Parenizor method (' setValue ', function (value) {
this. Value=http://www.script-home.com//article/value;
return this.
});
Parenizor. Method (' getValue ', function () {
return. This value;
});
Parenizor. Method (' toString ', function () {
return '(' + enclosing getValue () +') '.
});

the grammar may not be used, but it is very easy to see in the form of the class. Method accepts a method name, and a function, and put them in the class as a public methods.
now we can be written as

myParenizor=new Parenizor (0);
myString=myParenizor. ToString ();

as expected, myString is "(0)".
now we are going to build another inherited from Parenizor class, it's basically the same except for the toString method will produce "- 0 -" if the value is zero or null.

function ZParenizor (value) {
this. SetValue (value); .

} ZParenizor inherits (Parenizor);
ZParenizor. Method (e; "e; toString", function () {
if (this. GetValue ()) {
return this. Uber 'toString ();
}
return "- 0 -";
});

inherits method is similar to Java extends. Super uber method is similar to Java. It makes a method invocation of the parent class method (changed the name to avoid conflict and reserved words).
we can be written as

myZParenizor=new ZParenizor (0);
myString=myZParenizor. ToString ();

the myString is "0 -".
JavaScript and there is no class, but we can achieve this goal programming.
multiple inheritance
by operating a function prototype object, we can realize multiple inheritance. Mixed multiple inheritance is difficult to implement and could face the danger of name conflict. We can realize the mixture of multiple inheritance in JavaScript, but this example we will use a more normative form called Swiss inheritance SwissI nheritance.
suppose have a NumberValue class have a setValue method is used to check if a value within a specified range of a number, and an exception is thrown at the appropriate time. We as long as it's setValue and give our ZParenizor setRange method. Of course we don't want its toString method. In this way, we wrote:

ZParenizor. Swiss (NumberValue, 'setValue', 'setRange');

this will only add the necessary methods.
parasitic inheritance
this is another method of writing ZParenizor class. Not from Parenizor inheritance, but wrote a call Parenizor constructor's constructor, and modify the results finally return the results. The constructor add privileges method rather than the public.

function ZParenizor2 (value) {
var self=new Parenizor (value);
self. ToString=function () {
if (this. GetValue ()) {
return this. Uber 'toString ();

} return "- 0 -"
};
return Self;
}

class inheritance is a kind of "is..." Relationship, and parasitic inheritance is one of the "original is... but now..." The relationship between. In the construction of the object constructor played a large role. Attention uber (instead of the super keyword) for privilege method is still effective.
class extends
JavaScript dynamic can let us to an existing class add or replace method. We can call a method at any time. We can at any time to extend a class. Inheritance is not the way. So we call it "class extension" to avoid and Java extends ─ ─ also call extension, but is not the same thing ─ ─ confused.
object
expand in static object-oriented language, if you want an object to another object, you must set up a new class. But in JavaScript, you can add methods to the single object instead of a new class. It has great power because you can write the less as far as possible, class also can write more simple. Think of JavaScript object like a hash table. You can add new value at any time. If the value is a function, then he will become a method.
so in the example above, I don't need ZParenizor class. I just simple change on my example.

myParenizor=new Parenizor (0);
myParenizor. ToString=function () {
if (this. GetValue ()) {
return this. Uber 'toString ();

} return "- 0 -";
};
myString=myParenizor. ToString ();

we give myParenizor instance added a toString method without using any of the inheritance. We can evolution single instance because the language is no type.
small dessert
to make the example above to run, I wrote the four "dessert" method. First of all, the method method, you can add an instance method in a class.

Function. The prototype. The method=Function (name, func) {
this. Prototype [name]=func;
return this;
};

this will add a common approach to the Function. The prototype, so through the class extends all functions you can use it. It is a name and a function as a parameter.
it returns this. When I write a no return value method, I usually let it return to this. So that we can form a chain.
here are inherits method, it is pointed out that a class is inherited from another class. It must be defined only after the two classes are defined, but to call before method inheritance.

Function.method('inherits', function (parent) {
var d=0, p=(this.prototype=new parent());
this.method('uber', function uber(name) {
var f, r, t=d, 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 +=1;
r=f.apply(this, Array.prototype.slice.apply(arguments, [1]));
d -=1;
return r;
});
return this;
});

, we extend the Function class. We add an instance of the parent class and put it as a new prototype. We must also correct the constructor field, at the same time we join uber method.
uber method will find a method in your prototype. This is a kind of parasitic inheritance or class extends. If we are class inheritance, so we need to find the parent function in the prototype. Return statement invokes the function of the apply method to call the function, at the same time, explicitly to set this and pass parameters. Parameters (if any) can be obtained from the array of the arguments. Unfortunately, the arguments array is not a real array, so we will use the apply to invoke the slice method of the array.
in the end, the method of Swiss

Function. The method (' Swiss ', Function (the parent) {
the for (var I=1; I <the arguments. The length; I +=1) {
var name=the arguments [I];
this. Prototype [name]=parent. The prototype [name],
}
return this;
});

The Swiss method for loop for each parameter. Each name, it will be the parent of a member of the prototype copy down into the prototype of the new class.

summary JavaScript can be used as a kind of language, but it also has a very unique performance level. We have seen the class inheritance inheritance, Switzerland, parasitic inheritance, class extension and extended object. The first series of code reuse pattern can from this was thought to be very small, very simple JavaScript language.
class objects belong to the "hard". Add members to a "hard" object's the only way is to create a new class. In JavaScript, object is "soft". To add members to a "soft" object just simple assignment.
because JavaScript in the class is so flexible, you might also think of more complex class hierarchies. But the depth of the inheritance is not appropriate. Shallow inheritance is more effective and more expressive.


This concludes the body part