多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>继承</title> </head> <body> <script type="text/javascript"> // 1. 通过调用Person.call(this, name, age)继承Person的成员,但不包括原型的成员 function Person(name, age) { this.type = "human"; this.name = name; this.age = age; this.getName = function() { console.log(this.name) } } function Student(name, age) { // 继承Person所有的成员,但不包括原型的成员 Person.call(this, name, age); } var s1 = new Student("Jack", 21); console.log(s1.type, s1.name, s1.age); // human Jack 21 s1.getName(); // 21 </script> <script type="text/javascript"> // 2. 通过拷贝继承原型的成员 function Person(name, age) { this.type = "human"; this.name = name; this.age = age; } Person.prototype.getName = function() { console.log(this.name); } function Student(name, age) { // 继承Person所有的成员,但不包括原型的成员 Person.call(this, name, age); } for(var key in Person.prototype) { // 通过拷贝来继承原型的成员 Student.prototype[key] = Person.prototype[key]; } var s1 = new Student("Jack", 21); s1.getName(); // Jack </script> <script type="text/javascript"> // 3. 利用原型特性继承 function Person (name, age) { this.type = 'human' this.name = name this.age = age } Person.prototype.sayName = function () { console.log('hello ' + this.name) } function Student (name, age) { // 继承Person的所有成员,但不包括原型中的成员 Person.call(this, name, age) } // 利用原型的特性实现继承原型的所有成员 Student.prototype = new Person() var s1 = new Student('张三', 18) console.log(s1.type) // => human s1.sayName() // => hello 张三 </script> </body> </html> ```