javascript funciton.call 继承基类

function Person(name) //带参数的构造函数
{
    this.name = name; //将参数值赋给给this 对象的属性
    this.SayHello = function() {console.log("Hello, I'm " + this.name);}; //给this 对象定义一个SayHello 方法。
};

function Employee(name, salary) //子构造函数
{
    Person.call(this, name); //将this 传给父构造函数
    this.salary = salary; //设置一个this 的salary 属性
    this.ShowMeTheMoney = function() {console.log(this.name+" $" + this.salary);}; //添加ShowMeTheMoney 方法。
};

var BillGates = new Person("Bill Gates"); //用Person 构造函数创建BillGates 对象
var SteveJobs = new Employee("Steve Jobs", 1234); //用Empolyee 构造函数创建SteveJobs 对象

BillGates.SayHello(); //显示:I'm Bill Gates
SteveJobs.SayHello(); //显示:I'm Steve Jobs
SteveJobs.ShowMeTheMoney(); //显示:Steve Jobs $1234

console.log(BillGates.constructor == Person); //显示:true
console.log(SteveJobs.constructor == Employee); //显示:true

console.log(BillGates.SayHello == SteveJobs.SayHello); //显示:false
//为什么这是false???

 

posted @ 2013-02-03 21:31  feva  阅读(204)  评论(0编辑  收藏  举报