原型(prototype):每一个函数都有一个原型对象(属性对象),原型上面绑定是公有的属性或者方法,而且里面的this依然指向实例对象。
function Person(name, age, sex) {this.name = name;this.age = age;this.sex = sex;}
原型分开的写法
Person.prototype.showname = function() { //实例方法console.log(this.name);};Person.prototype.showage = function() { //实例方法console.log(this.age);};Person.prototype.showsex = function() { //实例方法console.log(this.sex);};
原型的合并写法
Person.prototype = {showname:function() {console.log(this.name);},showage:function() {console.log(this.age);},showsex:function() {console.log(this.sex);}};let p1 = new Person('zhangsan', 19, '女');p1.showname()p1.showage()p1.showsex()
