网易2017内推笔试题

要求:

  请实现下面的自定义事件Event对象的接口,功能见注释(测试1)

  该Event对象的接口需要能被其他对象拓展复用(测试2)

     //测试1
Event.on('test',function(result){
console.log(result);
});
Event.on('test',function(){
console.log('test');
});
Event.emit('test','hello world'); //输出'hello world' 和 'test'
//测试2
var person1 = {};
var person2 = {};
Object.assign(person1, Event);
Object.assign(person2, Event);
person1.on('call1',function(){
console.log('person1');
});
person2.on('call2',function(){
console.log('person2');
});
person1.emit('call1'); //输出'person1'
person2.emit('call2'); //没有输出
person1.emit('call1'); //没有输出
person2.emit('call2'); //输出'person2' var Event = {
//通过on接口监听事件eventName
//如果事件eventName被触发,则执行callback回调函数
on:function(eventName, callback){
//你的代码
},
//触发事件 eventName
emit:function(eventName){
//你的代码
}
};
  • Object.assign(target, ...sources)  可以把任意多个的源对象自身的可枚举属性拷贝给目标对象,然后返回目标对象。

这里有篇解决的……但是没看明白http://blog.daraw.cn/2016/08/02/javascript-event-emitter/

还是牛客高手多

         var Event = {
on: function(eventName, callback){
this[eventName] = this[eventName] || new Array();
this[eventName].push(callback);
}, emit: function(eventName){
var params = arguments.length>1 ? Array.prototype.slice.call(arguments,1) :[];
if(this[eventName]){
Array.prototype.forEach.call(this[eventName],function(arg){
arg.apply(this,params);
});
}
}
}

以上的方法实现过程中,遇到一些问题……

  1) 一开始想茬了,以为this[eventName]可以写作this.eventName,然而输出结果总是不对。然后才想到,this.eventName等于是调用了名为'eventName’的属性,而不是把eventName代表的字符串传给this的属性。所以失败了。

  2)一开始以为Array.prototype.slice/forEach可以写作Array.方法名,但是会报错。所以应该是只能通过prototype调用call/apply

  3)一开始没有理解这道题的意思。其实指的是:Event中的on绑定事件,而emit执行事件。一开始以为触发事件的代码:Event.emit('test','hello world'); 是传入了两个参数,一次传入test,一次传入hello world。其实完全想错了……很明显,'test’是一个eventName,是在test绑定的事件里传入参数hello world.而之所以输出hello world与test,是因为test绑了两个事件,一个是有参数的输出参数,一个是没有参数的输出test。所以这里是事件绑定而非赋值。

  4)关于Object.assign(). 其定义是这样的:

Object.assign(target, ...sources)

Properties in the object will be overwritten by properties in the sources if they have the same key.  Later sources' properties will similarly overwrite earlier ones.

The Object.assign() method only copies enumerable and own properties from a source object to a target object. It uses [[Get]] on the source and [[Set]] on the target, so it will invoke getters and setters. Therefore it assigns properties versus just copying or defining new properties. This may make it unsuitable for merging new properties into a prototype if the merge sources contain getters. For copying property definitions, including their enumerability, into prototypesObject.getOwnPropertyDescriptor() and Object.defineProperty() should be used instead.

Both String and Symbol properties are copied.

In case of an error, for example if a property is non-writable, a TypeError will be raised, and the targetobject remains unchanged.

Note that Object.assign() does not throw on null or undefined source values.

  即:将来自一个或多个源对象中的值复制到一个目标对象。

    此函数返回目标对象。

    仅可枚举自有属性从源对象复制到目标对象。

    可使用此函数合并或克隆对象。

    null 或 undefined 源被视为空对象一样对待,不会对目标对象产生任何影响。

  【以下是我的渣翻译】这里意思是说:对象中的属性会被源中的属性覆盖,后面的属性会被前面的覆盖。

  这种方法只会从源对象中复制可枚举的和自己的属性到一个目标对象。然后返回目标对象。

  如果要复制属性定义,包括其可枚举性,应该使用Object.getOwnPrototypeDescriptor()和Object.defineProperty()

  5)牛客网上答题的大神更新了答案,把this复制给一个变量self, var self = this,然后其后的this都用self代替,是【考虑到了参数及this作用域的改进版本】。但是这里并不理解,既然在这个函数里,this和self都指向一样啊?为何要这么做呢?

    我大概明白了……在函数里调用this可能会引发各种奇怪的问题,因为this的指向并不一定总是你想要指的this。但是用self应该也不恰当,因为“You should avoid self as there is a window.self object and you could end up using that accidentally if you forget to declare your own self var”——应该避免self因为有一个window.self对象。所以这里我改成了that。于是代码写作:

         var Event = {
on: function(eventName, callback){
if(!this[eventName]){
this[eventName] = [];
}
this[eventName].push(callback);
},
emit: function(eventName){
var that = this;
var params = arguments.length>1 ? Array.prototype.slice.call(arguments,1) : [];
if(that[eventName]){
Array.prototype.forEach.call(that[eventName],function(arg){
arg.apply(self,params);
});
}
}
}

js:实现自定义事件对象接口的更多相关文章

  1. js的自定义事件

    js中的事件是js的一大技术点,说白了就是操作dom树的唯一途径. 关于事件无非两种绑定方式: document.getElementById('xxx').onclick = function(){ ...

  2. JS中的事件&对象

    一.JS中的事件 (一)JS中的事件分类 1.鼠标事件 click/dblclick/onmouseover/onmouseout 2.HTML事件 onload/onscroll/onsubmit/ ...

  3. (转)js原生自定义事件的触发dispatchEvent

    1. 对于标准浏览器,其提供了可供元素触发的方法:element.dispatchEvent(). 不过,在使用该方法之前,我们还需要做其他两件事,及创建和初始化.因此,总结说来就是: 1 2 3 d ...

  4. js跨浏览器事件对象、事件处理程序

    项目中有时候会不用jquery这么好用的框架,需要自己封装一些事件对象和事件处理程序,像封装AJAX那样:这里面考虑最多的还是浏览器的兼容问题,原生js封装如下:var EventUtil={ //节 ...

  5. JS中的事件(对象,冒泡,委托,绑定)

    - 事件,是文档或浏览器窗口中发生的一些特定的交互瞬间,JS与HTML之间的交互是通过事件实现的 对于web应用来说,有下面这些代表性事件:点击事件,鼠标移动,按下键盘等等 - 事件,是用户和浏览器之 ...

  6. js中获取事件对象的方法小结

    原文地址:http://jingyan.baidu.com/article/d8072ac4594d6cec95cefdac.html 事件对象 的获取很简单,很久前我们就知道IE中事件对象是作为全局 ...

  7. 和我一起理解js中的事件对象

    我们知道在JS中常用的事件有: 页面事件:load: 焦点事件:focus,blur: 鼠标事件:click,mouseout,mouseover,mousemove等: 键盘事件:keydown,k ...

  8. js事件处理、事件对象

    事件类型分类: 1 添加在html结构中的事件 <div id="div1" onclick="alert('append click event in html' ...

  9. js中自定义事件,使用了jQuery

    $(function(){ $('#btn').bind("myClick", function(){ //自定义myClick事件 $('#test').append(" ...

随机推荐

  1. jquery 处理重新绑定插件的方法

    比如有一个slide的jquery插件,页面打开就对dom进行了绑定. <div class="expert"> <div class="expert- ...

  2. linux下iptables防火墙设置

    各位linux的爱好者或者工作跟linux相关的程序员,我们在工作中经常遇到应用服务器端口已经启动, 在网络正常的情况下,访问不到应用程序,这个跟防火墙设置有关 操作步骤 1.检查有没有启动防火墙 s ...

  3. code1002 搭桥

    最小生成树 每读入一个城市,把他与之前的所有城市做一次link() link的内容: 1.如果两个城市直接相连,合并他们的集合(并查集)2.如果两个城市可以搭桥,添加一条边来连接.如果不可以搭桥,什么 ...

  4. php用get方式传json数据 变成null了

    $data = I('param.data'); $data=stripslashes(html_entity_decode($data));//$data为传过去的json字符串

  5. springmvc 整合数据验证框架 jsr

    1.maven <dependency> <groupId>javax.validation</groupId> <artifactId>validat ...

  6. c++中如何定义编译期间常量,即这个常量可以用于定义数组下标

    在c++中,类里面的成员变量不仅仅可以被const修饰,还可以被static const修饰,此时一个内建类型(如int ,char ,long等)的static const 可以看做是一个编译期间的 ...

  7. Windows游戏找不到了怎么办?

         大家有的时候,可能是不慎操作,或是某些新装的Windows,会发现那些经典的游戏不见了,那它们去哪了呢?是长腿跑了?还是Windows偷工减料?都不是,让巩固来教你们把他们找出来! 1.在开 ...

  8. 2018.08.18 NOIP模拟 travel(贪心)

    Travel 题目背景 SOURCE:NOIP2015-SHY4 题目描述 小 A 要进行一次旅行.这回他要在序号为 1 到 n 的 n 个城市之间旅行.这 n 个城市之间共有 m 条连接两个城市的单 ...

  9. 用python实现各种排序算法

    最简单的排序有三种:插入排序,选择排序和冒泡排序.它们的平均时间复杂度均为O(n^2),在这里对原理就不加赘述了. 贴出源代码: 插入排序: def insertion_sort(sort_list) ...

  10. cuDNN

    https://developer.nvidia.com/developer-program https://developer.nvidia.com/cudnn cuda和cuDNN的关系 http ...