3.组合使用构造模式和原型模式

        //构造函数
        function Person(name,age,job){
            this.name=name;
            this.age=age;
            this.job=job;
            this.friends=['summer','xll']
        }
        Person.prototype={
            constructor:Person,
            sayName:function(){
                alert(this.name)
            }
        }
        var person1=new Person('Nico',23,'Doctor')
        var person2=new Person('Greg',13,'Teacher')
        //sayName()代表里面的返回值,sayName代表函数
        person1.friends.push('Van')
        alert(person1.friends)  //summer,xll,Nico
        alert(person2.friends)  //summer,xll
        alert(person1.friends===person2.friends)  //false
        alert(person1.sayName===person2.sayName)  //true
        console.log(person1.sayName)   // function(){alert(this.name)}
        console.log(person1.sayName()) //Nico

 

posted on 2017-03-03 11:22  懒人猫  阅读(166)  评论(0)    收藏  举报