1.写出 构造函数模式、混合模式、模块模式、工厂模式、单例模式、发布订阅模式的范例。
/*—————————————————————————————构造函数————————————————————————————————*/
function Person(name,age) {
this.name =name
this.age = age
}
Person.prototype.sayName = function() {
return this.name
}
var student = new Person('xiaoming',12)
/*————————————————————————————— 工厂模式————————————————————————————————*/
function createPerson(name) {
var person={
name:name,
sayName:function() {
console.log(this.name)
}
};
return person;
}
createPerson('zhangsan')
createPerson('lisi')
/*——————————————————————————————单例模式————————————————————————————————*/
var People = (function() {
var instance
function init(name) {
return {
name:name
};
}
return {
createPeople: function(name) {
if (!instance) {
instance = init(name)
}
return instance;
}
}
}())
People.createPeople('zhangsan')
/*————————————————————————————混合模式————————————————————————————————*/
var Person = function(name, age) {
this.name = name;
this.age = age;
};
Person.prototype.sayName = function(){
console.log(this.name);
}
var Student = function(name, age, score) {
Person.call(this, name, age);
this.score = score;
};
//Student.prototype = Object.create(Person.prototype);
Student.prototype = create(Person.prototype);
function create (parentObj){
function F(){}
F.prototype = parentObj;
return new F();
};
Student.prototype.sayScore = function(){
console.log(this.score);
}
var student = new Student("饥人谷", 28, 99);
console.log(student);
/*—————————————————————————————模块模式————————————————————————————————*/
var Person = (function(){
var name = 'ruoyu';
function sayName(){
console.log(name);
}
return {
name: name,
sayName: sayName
}
})()
/*———————————————————————————订阅发布模式————————————————————————————————*/
var PubSub = function() {
this.eventPool = {}
}
PubSub.prototype.on = function(topicName,callback) {
var topic = this.eventPool[topicName]
if (!topic) {
this.eventPool[topicName] = []
}
this.eventPool[topicName].push(callback)
}
PubSub.prototype.trigger = function() {
var _arg = [].slice.call(arguments)
var topicName = _arg.shift()
var callbackArg = _arg
var topicCallback = this.eventPool[topicName]
if (topicCallback) {
topicCallback.forEach(function(callback){
callback.apply(this,callbackArg)
})
}
}
PubSub.prototype.off = function() {
delete this.eventPool[topicName]
}
2.使用发布订阅模式写一个事件管理器,可以实现如下方式调用
var PubSub = function() {
this.eventPool = {}
}
PubSub.prototype.on = function(topicName,callback) {
var topic = this.eventPool[topicName]
if (!topic) {
this.eventPool[topicName] = []
}
this.eventPool[topicName].push(callback)
}
PubSub.prototype.fire = function() {
var _arg = [].slice.call(arguments)
var topicName = _arg.shift()
var callbackArg = _arg
var topicCallback = this.eventPool[topicName]
if (topicCallback) {
topicCallback.forEach(function(callback){
callback.apply(this,callbackArg)
})
}
}
PubSub.prototype.off = function(topicName) {
delete this.eventPool[topicName]
}
var Event = new PubSub()
Event.on('change', function(val){
console.log('change... now val is ' + val);
});
Event.fire('change', '饥人谷');
Event.off('changer');