thinking

front-end


  • Startseite

  • Archiv

  • Tags

solution to prototype_model's problem

Veröffentlicht am 2016-04-14

原型模式具有共享的本性,构造模式没有共享的本性可以传参

方法共享,属性不共享
实现方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function person(name,age,job){
this.name=name;
this.age=age;
this.job=job;
this.friends=["a","b","c"];
}
person.prototype={
constructor:person,
sayName:function(){
console.log(this.name);
}
};
var person1=new person("nichals","20","software engineer");
var person2=new person("ping","21","engineer");
person1.friends.push("d");/*通过实例1对数组修改*/
console.log(person1.name);
person1.sayName();
console.log(person2.name);
console.log(person2.friends);/*观察实例2的数组是否变化*/

1…171819…22
Ipo

Ipo

Do one thing to the best

22 Artikel
20 Tags
© 2016 Ipo
Erstellt mit Hexo
Theme - NexT.Pisces