js中对象的私有属性和公有属性

js中用function建立对象时this所建立的属性和方法是对象私有的,也就是所不一样的对象拥有不一样的拷贝,而prototype建立的属性和方法则是对象公有的,也就是所不一样的对象拥都指向同一份拷贝。this

    function person(name){spa

        this.name = nameprototype

        this.sayName = function(){对象

            console.log(dd);io

        }console

    }function

    person.prototype.age = 20;方法

    person.prototype.sayAge = function(){co

        console.log(this.age);new

    }

  var personObj1= new person('ly');

   var personObj2 = new person('ly');

     console.log(personObj1.sayName == personObj2.sayName); //false

    console.log(personObj1.sayAge == personObj2.sayAge); //true

相关文章
相关标签/搜索