/* 创建对象的最好方式:混合的构造函数/原型方式, *用构造函数定义对象的所有非函数属性,用原型方式定义对象的函数属性(方法) */ 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; } Student.prototype = new People(); 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
时间: 2024-10-12 18:53:15