组合使用构造函数和原型

function Person(name, age, job) {
            this.name = name;
            this.age = age;
            this.job = job;
            this.friends = ['Jack', 'Lee'];
        }
        Person.prototype = {
            constructor: Person,
            sayName:function () {
                return this.name + this.age + this.job;
            }
        };
        var person1 = new Person('a', 100, 'student');
        var person2 = new Person('b', 200, 'teacher');
        //alert(person1.sayName());
        //alert(person2.sayName());//b200
        person1.friends.push('lalala');
        alert(person1.friends);//jack lee lalala
        alert(person2.friends);//jack lee
        alert(person1.sayName == person2.sayName);//true.sayName是原型中的,因为有共享
        alert(person1.friends == person2.friends);//false。因为实例属性是在构造函数中定义;修改了person1的friends并不会影响person2的friends。因为不具备共享

  

原文地址:https://www.cnblogs.com/shenq/p/5458724.html