一、原型
二、继承
function Person(name){ this.name=name; } Person.prototype.eat=function(){ return "吃饭"; } function Student(name,sex){ Person.apply(this,[name]); this.sex=sex; } Student.prototype=Object.create(Person.prototype); Student.prototype.study=function(){ return "学习"; } var student=new Student("小明","男"); console.log(student.name); console.log(student.sex); console.log(student.eat()); console.log(student.study());
1、构造函数的属性放在对象上,函数放在原型上;
2、继承属性【Person.apply(this,[name]);】,继承方法【Student.prototype=Object.create(Person.prototype);】;
原文:https://www.cnblogs.com/linding/p/12530365.html