javascript中有原型对象和实例对象
如有疑问请参考:http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_encapsulation.html
下面的代码可以用Rhino解释,代码如下:
/** * Cat的对象 * name、color;猫科动物,吃老鼠 */ print("--- --- --- 生成对象的原始模式 --- ---"); /*var Cat = { name : '', color : '' };*/ var cat1 = {}; cat1.name = "mao1"; cat1.colr = "orange"; print("cat1 : name=" + cat1.name + ", color=" + cat1.colr); print(); print("--- --- --- 原始模式的改进 --- ---"); function Cat1(name,color){ return { name:name, color:color }; }; var cat2 = Cat1("mao2", "yellow"); print("cat2 : name=" + cat2.name + ", color=" + cat2.color); print(); print("--- --- --- 构造函数模式 --- ---"); function Cat2(name, color){ this.name = name; this.color = color; } var cat3 = new Cat2("mao3", "blue"); print("cat3 : name=" + cat3.name + ", color=" + cat3.color); print("cat3.constructor == Cat2 --- " + (cat3.constructor == Cat2)); print("cat3 instanceof Cat2 --- --- " + (cat3 instanceof Cat2)); print(); print("--- --- --- 构造函数模式的问题 --- ---"); function Cat3(name, color){ this.name = name; this.color = color; this.type = "猫科动物"; this.eat = function(){ print("吃老鼠"); }; } var cat4 = new Cat3("mao4", "red"); var cat5 = new Cat3("mao5", "pink"); print("cat4 : name=" + cat4.name + ", color=" + cat4.color + ", type=" + cat4.type); cat4.eat(); print("cat5 : name=" + cat5.name + ", color=" + cat5.color + ", type=" + cat5.type); cat5.eat(); print("cat4.type == cat5.type --- --- " + (cat4.type == cat5.type)); print("cat4.eat == cat5.eat() --- --- " + (cat4.eat == cat5.eat)); print(); print("--- --- --- Prototype模式 --- ---"); function Cat4(name,color){ this.name = name; this.color = color; } Cat4.prototype.type = "猫科动物"; Cat4.prototype.eat = function(){ print("吃老鼠"); }; var cat6 = new Cat4("mao6", "green"); var cat7 = new Cat4("mao7", "gray"); print("cat6 : name=" + cat6.name + ", color=" + cat6.color + ", type=" + cat6.type); cat6.eat(); print("cat7 : name=" + cat7.name + ", color=" + cat7.color + ", type=" + cat7.type); cat7.eat(); print("cat6.type == cat7.type --- --- " + (cat6.type == cat7.type)); print("cat6.eat == cat7.eat --- --- - " + (cat6.eat == cat7.eat)); print(); print("--- --- --- Prototype模式的验证方法 --- ---"); print("Cat4.prototype.isPrototypeOf(cat6) --- " + Cat4.prototype.isPrototypeOf(cat6)); print("Cat4.prototype.isPrototypeOf(cat5) --- " + Cat4.prototype.isPrototypeOf(cat5)); print('cat6.hasOwnProperty("name") --- --- ' + cat6.hasOwnProperty("name")); print('cat6.hasOwnProperty("type") --- --- ' + cat6.hasOwnProperty("type")); print('"name" in cat7 --- ---' + ("name" in cat7)); print('"colr" in cat7 --- ---' + ("colr" in cat7)); for(var prop in cat7) { print("\tcat7["+prop+"] = " + cat7[prop]); }
打印结果如下:
--- --- --- 生成对象的原始模式 --- --- cat1 : name=mao1, color=orange --- --- --- 原始模式的改进 --- --- cat2 : name=mao2, color=yellow --- --- --- 构造函数模式 --- --- cat3 : name=mao3, color=blue cat3.constructor == Cat2 --- true cat3 instanceof Cat2 --- --- true --- --- --- 构造函数模式的问题 --- --- cat4 : name=mao4, color=red, type=猫科动物 吃老鼠 cat5 : name=mao5, color=pink, type=猫科动物 吃老鼠 cat4.type == cat5.type --- --- true cat4.eat == cat5.eat() --- --- false --- --- --- Prototype模式 --- --- cat6 : name=mao6, color=green, type=猫科动物 吃老鼠 cat7 : name=mao7, color=gray, type=猫科动物 吃老鼠 cat6.type == cat7.type --- --- true cat6.eat == cat7.eat --- --- - true --- --- --- Prototype模式的验证方法 --- --- Cat4.prototype.isPrototypeOf(cat6) --- true Cat4.prototype.isPrototypeOf(cat5) --- false cat6.hasOwnProperty("name") --- --- true cat6.hasOwnProperty("type") --- --- false "name" in cat7 --- ---true "colr" in cat7 --- ---false cat7[name] = mao7 cat7[color] = gray cat7[type] = 猫科动物 cat7[eat] = function () { print("\u5403\u8001\u9f20"); }