javascript class inheritance
Author:Eve Cole
Update Time:2009-06-11 16:23:07
1. The first way is to pretend to be an object. (Using that every method name in js is a Function object) Java code function Parent(username){
this.username = username;
this.say = function(){
alert(this.username);
}
}
function Child(username,password){
this.temp = Parent;//temp points to where Parent points. Each method name in js is a Function object, pointing to a method.
this.temp(username);//The content in the initialization method delete this.temp;//temp is no longer useful. You can delete it directly. This cannot be lost //Parent(username);//Writing this way may seem correct on the surface, but it is actually wrong. Because only objects that come out of new have this, there is no value when calling this in Parent this.password = password;
this.hello = function(){
alert(this.password);
}
}
var parent = new Parent("zhangsan");
parent.say();//zhangsan
var child = new Child("lisi","123456");
child.say();//lisi
child.hello();//123456
//The first way is to pretend to be an object. (Using that every method name in js is a Function object)
function Parent(username){
this.username = username;
this.say = function(){
alert(this.username);
}
}
function Child(username,password){
this.temp = Parent;//temp points to where Parent points. Each method name in js is a Function object, pointing to a method.
this.temp(username);//The content in the initialization method delete this.temp;//temp is no longer useful. You can delete it directly. This cannot be lost //Parent(username);//Writing this way may seem correct on the surface, but it is actually wrong. Because only objects that come out of new have this, there is no value when calling this in Parent this.password = password;
this.hello = function(){
alert(this.password);
}
}
var parent = new Parent("zhangsan");
parent.say();//zhangsan
var child = new Child("lisi","123456");
child.say();//lisi
child.hello();//123456
2. The second method: call() function method The call() function is a function of the Function object. The specific usage is as follows Java code // The call() function is a function of the Function object // The specific usage is such as function test(str){
alert(this.username + "," + str);
}
var o = new Object();
o.username = "zhangsan";
test.call(o,"123456");//zhangsan,123456. Because each Function object has a call() method, and the function name is a Function object. The first parameter of the call() function is the test function This in.
//The call() function is a function of the Function object //The specific usage is such as function test(str){
alert(this.username + "," + str);
}
var o = new Object();
o.username = "zhangsan";
test.call(o,"123456");//zhangsan,123456. Because each Function object has a call() method, and the function name is a Function object. The first parameter of the call() function is the test function This in.
Java code function Parent(username){
this.username = username;
this.say = function(){
alert(this.username);
}
}
function Child(username,password){
Parent.call(this,username);
this.password = password;
this.hello = function(){
alert(this.password);
}
}
var parent = new Parent("zhangsan");
parent.say();//zhangsan
var child = new Child("lisi","123456");
child.say();//lisi
child.hello();//123456
function Parent(username){
this.username = username;
this.say = function(){
alert(this.username);
}
}
function Child(username,password){
Parent.call(this,username);
this.password = password;
this.hello = function(){
alert(this.password);
}
}
var parent = new Parent("zhangsan");
parent.say();//zhangsan
var child = new Child("lisi","123456");
child.say();//lisi
child.hello();//123456
3. The third implementation method: apply() function method apply() and call() are the same, except that the parameters are passed differently. The parameters of apply are array Java codes // The third implementation method: apply() function Method, apply() and call() are the same, except that the parameters are passed differently. The parameters of apply are arrays // so inheritance can implement function Parent(username){ like this
this.username = username;
this.say = function(){
alert(this.username);
}
}
function Child(username,password){
Parent.apply(this,new Array(username));
this.password = password;
this.hello = function(){
alert(this.password);
}
}
var parent = new Parent("zhangsan");
parent.say();//zhangsan
var child = new Child("lisi","123456");
child.say();//lisi
child.hello();//123456