您的位置:首页 > Web前端 > JavaScript

js 继承--call方法

2008-12-05 17:05 295 查看
var Person = function (name)
{
this.name = name;
this.ShowName = function()
{
alert(this.name);
}
};

var Employee = function(name,salary)
{
this.salary = salary;
Person.call(this,name);
};
Employee.prototype = new Person();  //建一个基类的对象作为子类原型的原型,这里很有意思
Employee.prototype.ShowMeTheMoney = function()  //给子类添构造函数的prototype添加方法
{
alert(this.name + " $" + this.salary);
};

var NbaEmployee = function(name,salary,age)
{
this.age = age;
Employee.call(this,name,salary);
};
NbaEmployee.prototype = new Employee();
NbaEmployee.prototype.ShowAge = function()
{
alert(this.age);
};

var BillGates = new Person("Bill Gates");   //创建基类Person的BillGates对象
var SteveJobs = new Employee("Steve Jobs", 1234);   //创建子类Employee的SteveJobs对象

BillGates.ShowName();       //通过对象直接调用到prototype的方法
SteveJobs.ShowName();       //通过子类对象直接调用基类prototype的方法,关注!
SteveJobs.ShowMeTheMoney(); //通过子类对象直接调用子类prototype的方法

var Kobe = new NbaEmployee("Kobe", "2000W",29);
Kobe.ShowName();

Kobe.ShowMeTheMoney();

Kobe.ShowAge();



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: