/* 创建对象的一种方式:混合的构造函数/原型方式, *用构造函数定义对象的所有非函数属性,用原型方式定义对象的函数属性(方法) */ function People(sname){ this.name = sname; } People.prototype.sayName = function(){ console.log(this.name); } /* 一种继承机制: * 用对象冒充继承构造函数的属性,用原型prototype继承对象的方法。 */ function Student(sname,sage){ People.call(this,sname); this.age = sage; } //临时构造函数模式(圣杯模式)
var F = new Function(){} ;
F.prototype = People.prototype ;
Student.prototype = new F() ;
Student.prototype.sayAge = function(){ console.log(this.age); };
//实例
var people1 = new People("jeff");
people1.sayName(); //输出:jeff
var student1 = new Student("john",30);
student1.sayName(); //输出:john
student1.sayAge(); //输出:30