1、寫出 構造函數模式、混合模式、模塊模式、工廠模式、單例模式、發布訂閱模式的范例。
1、設計模式分類:
-
構造函數模式(constructor)
含義:創建一個構造函數
function Person(name,age){
this.name=name;
this.age=age;
}
Person.prototype.sayName=function(){
return this.name;
}
var student=new Person("Ethan",28);
-
工廠模式(factory)
含義:通常是一個函數return出來一個新的對象
function createPerson(name){
var person={
name: name,
sayName:function(){
return this.name;
}
}
return person;
}
createPerson("Ethan");
this回顧:
1、方法調用模式:createPerson("Ethan").sayName(); this指向person
2、函數調用模式: var t=createPerson("Ethan").sayName; this指向window
3、new構造對象:this指向新構造的對象
4、自動指向:apply,call,bind方法
-
單例模式(singleton)
含義:每次調用這個模式不會去創造,有且只有一個(例子:組件生成的對話框,一個頁面只出現一次)
var People=(function(){
var instance;
function init(name){
return {
name: name
}
}
return {
createPeople:function(name){
if(!instance){
instance=init(name);
}
return instance;
}
}
})();
//函數作用域:詞法作用域,js作用域由function所體現,function訪問的上下文由它所在定義的位置所決定
People.createPeople("Ethan");
People.createPeople("age");
-
混合模式(mixin)
含義:對某個對象,增加新的東西(一般都用來混合它的原型)
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)
含義:一般通過閉包的方式實現的,特點:避免全局變量的沖突
var Person=(function(){
var name="Ethan";
function sayName(){
console.log(name);
}
return: {
name: name,
sayName:sayName
}
})();
-
訂閱發布模式(subcribe/publish)
含義:先訂閱后發布,相當于事件監控模式,先定義好函數,再在任何時候異步同步觸發事件
var EventCenter=(function(){
var events={}; //存儲所有的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、使用發布訂閱模式寫一個事件管理器,可以實現如下方式調用
Event.on('change', function(val){
console.log('change... now val is ' + val);
});
Event.fire('change', 'Ethan');
Event.off('changer');
var Event=(function(){
var events={}; //存儲所有的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)