UNPKG

0216tool_xiaoye

Version:

叶永洁自定义工具函数库

48 lines (40 loc) 1.3 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>手写继承</title> </head> <body> <script> function Person (name,age){ console.log('person') this.name = name this.age = age } Person.prototype.sayName = function (){ console.log(`我的名字是${this.name}`) } function Student (name,age,price){ //借用父类的构造函数 Person.call(this,name,age) this.price = price } //创建一个指定原型对象的任意类型的实例 function create (proto){ function Fn (){} //将Person的原型对象指定为Fn的原型对象 Fn.prototype = proto return new Fn() } //将子类的原型对象是Person的实例 Student.prototype = create(Person.prototype) //修改子类的构造器指向自身 Student.prototype.constructor = Student let s = new Student('叶',20,200000) console.log(s) console.log(s.constructor) s.sayName() </script> </body> </html>