1兜畸、寫出 構(gòu)造函數(shù)模式、混合模式碘梢、模塊模式咬摇、工廠模式、單例模式煞躬、發(fā)布訂閱模式的范例肛鹏。
1、設(shè)計(jì)模式分類:
-
構(gòu)造函數(shù)模式(constructor)
含義:創(chuàng)建一個(gè)構(gòu)造函數(shù)
function Person(name,age){
this.name=name;
this.age=age;
}
Person.prototype.sayName=function(){
return this.name;
}
var student=new Person("Ethan",28);
-
工廠模式(factory)
含義:通常是一個(gè)函數(shù)return出來一個(gè)新的對(duì)象
function createPerson(name){
var person={
name: name,
sayName:function(){
return this.name;
}
}
return person;
}
createPerson("Ethan");
this回顧:
1恩沛、方法調(diào)用模式:createPerson("Ethan").sayName(); this指向person
2在扰、函數(shù)調(diào)用模式: var t=createPerson("Ethan").sayName; this指向window
3、new構(gòu)造對(duì)象:this指向新構(gòu)造的對(duì)象
4雷客、自動(dòng)指向:apply,call,bind方法
-
單例模式(singleton)
含義:每次調(diào)用這個(gè)模式不會(huì)去創(chuàng)造芒珠,有且只有一個(gè)(例子:組件生成的對(duì)話框,一個(gè)頁面只出現(xiàn)一次)
var People=(function(){
var instance;
function init(name){
return {
name: name
}
}
return {
createPeople:function(name){
if(!instance){
instance=init(name);
}
return instance;
}
}
})();
//函數(shù)作用域:詞法作用域搅裙,js作用域由function所體現(xiàn)皱卓,function訪問的上下文由它所在定義的位置所決定
People.createPeople("Ethan");
People.createPeople("age");
-
混合模式(mixin)
含義:對(duì)某個(gè)對(duì)象,增加新的東西(一般都用來混合它的原型)
function People(name,age){
this.name=name;
this.age=age;
}
People.prototype.sayName=function(){
console.log(this.name);
}
var Student=function(name,age,score){
People.call(this,name,age);
this.score=score;
}
//用原生js方法將student.prototype指向People
Student.prototype=create(People.prototype);
function create(parentObj){
function F(){}
F.prototype=parentObj;
return new F;
}
Student.prototype.sayScore=function(){
console.log(this.score);
}
var student=new Student("Ethan","28","97")
-
模塊模式(module)
含義:一般通過閉包的方式實(shí)現(xiàn)的部逮,特點(diǎn):避免全局變量的沖突
var Person=(function(){
var name="Ethan";
function sayName(){
console.log(name);
}
return: {
name: name,
sayName:sayName
}
})();
-
訂閱發(fā)布模式(subcribe/publish)
含義:先訂閱后發(fā)布娜汁,相當(dāng)于事件監(jiān)控模式,先定義好函數(shù)兄朋,再在任何時(shí)候異步同步觸發(fā)事件
var EventCenter=(function(){
var events={}; //存儲(chǔ)所有的key/value
//events={"hello":[{function(){}:function(){}},{function(){}:function(){}},...]}
function on(evt,handle){
events[evt]=events[evt] || [];
events[evt].push({
handle:handle
});
//events["hello"]=[{handle:handle}]
}
function fire(evt,args){
if(!events[evt]) return;
for(var i=0;i<events[evt].length;i++){
events[evt][i].handle(args);
}
}
function off(name){
delete events[name];
}
return {
on:on,
fire:fire,
off:off
};
})();
EventCenter.on("hello",function(word){
console.log(word);
});
EventCenter.fire("hello","word");
2掐禁、使用發(fā)布訂閱模式寫一個(gè)事件管理器,可以實(shí)現(xiàn)如下方式調(diào)用
Event.on('change', function(val){
console.log('change... now val is ' + val);
});
Event.fire('change', 'Ethan');
Event.off('changer');
var Event=(function(){
var events={}; //存儲(chǔ)所有的key/value
//events={"hello":[{function(){}:function(){}},{function(){}:function(){}},...]}
function on(evt,handle){
events[evt]=events[evt] || [];
events[evt].push({
handle:handle
});
//events["hello"]=[{handle:handle}]
}
function fire(evt,args){
if(!events[evt]) return;
for(var i=0;i<events[evt].length;i++){
events[evt][i].handle(args);
}
}
function off(name){
delete events[name];
}
return {
on:on,
fire:fire,
off:off
};
})();
Event.on('change', function(val){
console.log('change... now val is ' + val);
});
Event.fire('change', 'Ethan');
Event.off('change');
(mission 6)