Js prototype 实现继承
Person类:
function Person(name,age){ this.name = name; this.age = age; }
Prototype 继承:
Person.prototype.showName = function(){ console.log("my Name is: " + this.name); }
输出:
var people = new Person("chams",22); people.showName(); //out: my Name is: chams
Student类:
function Student() {}
Prototype 继承:
Student.prototype = new Person("chamsStudent",22); var student = new Student(); Student.prototype.height = "173"; Student.prototype.showHeight = function(){ console.log("my Height is: " + this.height); }
输出:
student.showName(); //out: my Name is: chamsStudent student.showHeight(); //out: my Height is: 173

浙公网安备 33010602011771号