js 继承--call方法

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();

posted on 2008-12-05 17:05  gotolovo  阅读(197)  评论(0编辑  收藏  举报