原型(prototype):每一个函数都有一个原型对象(属性对象),原型上面绑定是公有的属性或者方法,而且里面的this依然指向实例对象。

    1. function Person(name, age, sex) {
    2. this.name = name;
    3. this.age = age;
    4. this.sex = sex;
    5. }

    原型分开的写法

    1. Person.prototype.showname = function() { //实例方法
    2. console.log(this.name);
    3. };
    4. Person.prototype.showage = function() { //实例方法
    5. console.log(this.age);
    6. };
    7. Person.prototype.showsex = function() { //实例方法
    8. console.log(this.sex);
    9. };

    原型的合并写法

    1. Person.prototype = {
    2. showname:function() {
    3. console.log(this.name);
    4. },
    5. showage:function() {
    6. console.log(this.age);
    7. },
    8. showsex:function() {
    9. console.log(this.sex);
    10. }
    11. };
    12. let p1 = new Person('zhangsan', 19, '女');
    13. p1.showname()
    14. p1.showage()
    15. p1.showsex()