2016网易前端笔试附加题我的答案分享

var Event = {
on: function(eventName, callback){
//todo
if(!this[eventName])
this[eventName] = [];
this[eventName].push(callback);
},
emit: function(eventName){
//todo
if(this[eventName])
Array.prototype.forEach.call(this[eventName], function(func){
func.call(this);
});
}
}

Event.on("one", function(){
console.log("go");
})
Event.on("one", function(){
console.log("gogo");
})

Event.emit("one");

var person1 = Object.assign({}, Event);
var person2 = Object.assign({}, Event);
person1.on("666", function(){
console.log("person1 666");
})
person2.on("777", function(){
console.log("person2 777");
})

person1.emit("666");
person1.emit("777");
person2.emit("777");
person2.emit("666");
#网易##前端工程师#
全部评论
涉及兼容性和参数问题 var Event = {    on : function(eventName, callback){        this[eventName] = this[eventName] || new Array();        this[eventName].push(callback);    },    emit : function(eventName){      if(this[eventName]){        var i, func;        for(i = 0; func = this[eventName][i]; i++){          if(func.length <= 0){            func.call(this);          }else{            var args = Array.prototype.slice.call(arguments, 1);            func.apply(this, args);          }        }      }else{        return console.log(undefined);      }    } }
点赞 回复 分享
发布于 2016-08-04 14:52
感谢分享,
点赞 回复 分享
发布于 2016-08-03 23:48
感谢分享。
点赞 回复 分享
发布于 2016-08-03 21:19

相关推荐

野猪不是猪🐗:我assume that你must技术aspect是solid的,temperament也挺good的,however面试不太serious,generally会feel style上不够sharp
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务