脚本宝典收集整理的这篇文章主要介绍了js實例教程-JavaScript從工廠模式到寄生對象模式的代碼實例學習,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。小寶典致力於為廣大程序猿(媛)提供高品質的代碼服務,請大家多多光顧小站,小寶典在此謝過。
工廠模式:
function PErsonFactory(username, password) {
VAR person = new Object();
person.username = username;
person.password = password;
return person;
}
var person = personFactory("luohao", "123456");
console.LOG(person.username);
console.log(person.password);
寄生構造模式
function MyArray() {
var cITies = new Array();
cities.push.apply(cities, arguments);
cities.toMyString = function() {
}
return cities;
}
var cities = MyArray("西安", "武漢", "南京");
console.log(cities.toMyString());
工廠模式和寄生構造模式沒有任何區別。
一般使用工廠模式創建簡單的對象,使用寄生構造模式為不可以修改的對象添加方法。
無論是工廠模式還是寄生構造模式返回的對象都和聲明的對象在類型上沒有必然的關係。
因此不能使用instanceof 去判斷一個寄生構造模式返回的對象是不是就是寄生對象。
工廠模式:
function personFactory(username, password) {
var person = new Object();
person.username = username;
person.password = password;
return person;
}
var person = personFactory("luohao", "123456");
console.log(person.username);
console.log(person.password);
寄生構造模式
function MyArray() {
var cities = new Array();
cities.push.apply(cities, arguments);
cities.toMyString = function() {
return this.join('|');
}
return cities;
}
var cities = MyArray("西安", "武漢", "南京");
console.log(cities.toMyString());
工廠模式和寄生構造模式沒有任何區別。
一般使用工廠模式創建簡單的對象,使用寄生構造模式為不可以修改的對象添加方法。
無論是工廠模式還是寄生構造模式返回的對象都和聲明的對象在類型上沒有必然的關係。
因此不能使用instanceof 去判斷一個寄生構造模式返回的對象是不是就是寄生對象。
覺得可用,就經常來吧!Javascript技巧 腳本寶典 歡迎評論哦! js技巧,巧奪天工,精雕玉琢。小寶典獻醜了!
以上是脚本宝典为你收集整理的js實例教程-JavaScript從工廠模式到寄生對象模式的代碼實例學習全部内容,希望文章能够帮你解决js實例教程-JavaScript從工廠模式到寄生對象模式的代碼實例學習所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。