javascript学习笔记(九)对象--设计模式
1.创建对象
View Code
1 var person = new Object();
2 person.name = "RuiLiang";
3 person.age = 30;
4 person.job = "Teacher";
5 person.sayName = function () {
6 alert(this.name);
7 };
8
9 person.sayName();
2.工厂模式
缺点:不能识别对象
View Code
1 function createPerson(name,age,job) {
2 var o = new Object();
3 o.name = name;
4 o.age = age;
5 o.job = job;
6 o.sayName = function () {
7 alert(this.name);
8 };
9 return o;
10 }
11
12 var person1 = createPerson("阿亮",30,"教师");
13 var person2 = createPerson("俊俊",24,"待业");
14
15 person1.sayName(); //"阿亮"
16 person2.sayName(); //“俊俊”
3.构造函数模式
缺点:缺少封装性
View Code
1 function Person(name,age,job) {
2 this.name = name;
3 this.age = age;
4 this.job = job;
5 this.sayName = sayName;
6 }
7 function sayName() {
8 alert(this.name);
9 }
10
11 var person1 = new Person("阿亮",30,"教师");
12 var person2 = new Person("俊俊",24,"待业");
13 person1.sayName();
14 person2.sayName();
4.原型模式
缺点:所有属性被实例共享
View Code
1 function Person() {
2 }
3
4 Person.prototype.name = "ALiang";
5 Person.prototype.age = 30;
6 Person.prototype.job = "Teacher";
7 Person.sayName = function () {
8 alert(this.name);
9 }
hasOwnProperty()方法检测某一属性是不是实例属性,如果是返回 true
person1.hasOwnProperty("name"); //name是不是person1的属性
in 操作符:通过对象访问的属性是否存在,若存在返回 true,不管属性存在实例中还是原型中
alert("name" in person1); //name属性若存在返回 true
确定属性在原型中还是对象中的方法:
1 function hasPrototypeProperty(object,name) {
2 return !object.hasOwnProperty(name) && (name in object);
3 }
4 //用法
5 var person = new Person();
6 alert(hasPrototypeProperty(person,"name")); //true
7 person.name = "Grey"; //改变原型中name的值
8 alert(hasPrototypeProperty(person,"name")); //false
isPrototypeOf()方法是用来判断指定对象object1是否存在于另一个对象object2的原型链中,是则返回true,否则返回false。
格式如下:
object1.isPrototypeOf(object2);
object1是一个对象的实例;
object2是另一个将要检查其原型链的对象。
原型链可以用来在同一个对象类型的不同实例之间共享功能。
如果 object2 的原型链中包含object1,那么 isPrototypeOf 方法返回 true。
如果 object2 不是一个对象或者 object1 没有出现在 object2 中的原型链中,isPrototypeOf 方法将返回 false。
1 //字面量重写原型对象
2 function Person(){
3 }
4
5 Person.prototype = {
6 constructor : Person,
7 name : "ALiang",
8 age : 30,
9 job : "Teacher",
10 sayName : function() {
11 alert(this.name);
12 }
13 };
5.构造函数和原型混合模式
具有构造函数模式和原型模式的优点,属性用构造函数模式,方法用原型模式 //这种模式使用最广泛
View Code
1 function Person(name,age,job) {
2 this.name = name;
3 this.age = age;
4 this.job = job;
5 this.friends = ["xuyun","wuxueming"];
6 }
7 Person.prototype = {
8 constructor : Person,
9 sayName : function() {
10 alert(this.name);
11 }
12 };
13
14 var person1 = new Person("ALiang",30,"Teacher");
15 var person2 = new Person("ZuNan",26,"Teacher");
16 person1.friends.push("JunJun");
17 alert(person1.friends); //"xuyun","wuxueming","JunJun"
18 alert(person2.friends); //"xuyun","wuxueming"
6.动态原型模式
View Code
1 function Person(name,age,job) {
2 this.name = name;
3 this.age = age;
4 this.job = job;
5
6 if (typeof this.sayName != "function"){ //这里的sayName可以是任何初始化后存在的方法或属性
7 Person.prototype.sayName = function() { //不能用字面量形式
8 alert(this.name);
9 };
10 }
7.寄生构造函数模式
8.稳妥构造函数模式
摘自 晴天漫步
补充:web前端 , JavaScript ,