原型模型需要一个构造函数来定义对象的成员

//定义构造函数
function Person(name)
{
  this.name = name; //在构造函数中定义成员
};
//方法定义到构造函数的prototype 上
Person.prototype.SayHello = function()
{
  alert("Hello, I'm " + this.name);
};
//子类构造函数
function Employee(name, salary)
{
  Person.call(this, name); //调用上层构造函数
  this.salary = salary; //扩展的成员
};
//子类构造函数首先需要用上层构造函数来建立prototype 对象,实现继承的概念
Employee.prototype = new Person() //只需要其prototype 的方法,此对象的成员没有任何意义!
//子类方法也定义到构造函数之上
Employee.prototype.ShowMeTheMoney = function()
{
alert(this.name + " $" + this.salary);
};
var BillGates = new Person("Bill Gates");
BillGates.SayHello();
var SteveJobs = new Employee("Steve Jobs", 1234);
SteveJobs.SayHello();
SteveJobs.ShowMeTheMoney();

 

posted @ 2013-02-03 22:11  feva  阅读(145)  评论(0编辑  收藏  举报