hasownproperty

Use ES6 proxy to trap Object.hasOwnProperty

*爱你&永不变心* 提交于 2019-12-13 15:46:10
问题 I want to use an ES6 proxy to trap the following common code: for (let key in trapped) { if (!Object.prototype.hasOwnProperty.call(obj, key)) continue; let value = trapped[key]; //various code } But after reviewing the proxy documentation, I'm not sure how to do it, mainly because the has trap trap is for the in operator, which does not seem to be used in the above code and there is no trap for the hasOwnProperty operation. 回答1: You can use the getOwnPropertyDescriptor handler to capture

Is there a jQuery way of iterating over an objects own properties only?

别等时光非礼了梦想. 提交于 2019-12-10 15:22:20
问题 I'm making a small jQuery-like library, and one thing striking me odd is the behavior of $.each. In javascript we have a for...in loop: for (var key in obj) { console.log(key + ': ' + obj[key]); } The problem with this, is that it will iterate over inherited properties as well, that is, properties coming from the object constructor's prototype. One can know this using hasOwnProperty, for example. And jQuery could do that. But, when you pass an object to $.each , it behaves exactly like a for.

获取对象属性(所有属性、可枚举、不可枚举、自身属性【非原型链继承】)个数详解

微笑、不失礼 提交于 2019-12-09 22:31:39
一、获取可枚举的属性 方法一:for......in 方法一: Object.keys() Object.keys() 方法会返回一个由一个给定对象的自身可枚举属性组成的数组,数组中属性名的排列顺序和使用 for...in 循环遍历该对象时返回的顺序一致 。 语法 Object.keys( obj ) 参数 obj:要返回其枚举自身属性的对象。 返回值 一个表示给定对象的所有可枚举属性的字符串数组。 例子 // simple array var arr = ['a', 'b', 'c']; console.log(Object.keys(arr)); // console: ['0', '1', '2'] // array like object var obj = { 0: 'a', 1: 'b', 2: 'c' }; console.log(Object.keys(obj)); // console: ['0', '1', '2'] // array like object with random key ordering var anObj = { 100: 'a', 2: 'b', 7: 'c' }; console.log(Object.keys(anObj)); // console: ['2', '7', '100'] // getFoo is a property

websocket 心跳包重连

三世轮回 提交于 2019-12-07 15:46:27
上次我们讲过了websocket断线重连的问题,那么久会有人提出疑问了,心跳包重连跟断线重连有什么区别呢? 其实这两个都是为了达到一个目的,那就是保证当前设备的网络状态保持通畅。。。而断线重连呢,只能保证网络失去连接的时候有效,并不能保证网络断开的时候有效。。。这么说可能就有很多人迷糊了,这两者之间有什么区别呢? 其实很简单哈,至少我是这么理解的。网络失去连接的时候是你手动关闭网络或禁用网络时,这个时候会触发到websocket中的onclose事件,也就是说他会触发断线重连, 而网络断开的时候呢,指的是比较简单粗暴的方法,例如直接拔网线之类的。。。。而这个时候呢是不会触发onclose事件的,那这个时候我们要怎么办呢,我们就需要用到心跳重连了 好啦,说了这么多,现在我们直接来看代码吧。。。。 首先是断线重连,这个我们就不说了,如果没明白的可以去看我写的另外一篇关于断线重连的文章,这里我就不多说了。。。。 那么我们重点来讲一下心跳包重连 if (jsonGotData.hasOwnProperty('id')) { timestampVal = new Date(jsonGotData.now_time); heartbeatLive(webSocket, timestampVal); } if (jsonGotData.hasOwnProperty('heartbeat')) {

Array filter returns strange results

China☆狼群 提交于 2019-12-06 06:59:39
Related to this question, i wanted to try out this var arr = [0,1,2,true,4,{"abc":123},6,7,{"def":456},9,[10]]; arr.filter(Object.hasOwnProperty,"abc");//outputs [0, 1, 2] arr.filter(Object.hasOwnProperty,"2222222") //[0, 1, 2, 4, 6] Does anyone knows why filter return these values? Spec of filter and MDN doc also doesn't clearly tell how second argument of filter is used. The second argument to the Array.prototype.filter is the value that will be set as this to the function that is passed as a first argument. So your code ends up to be something like: arr.filter(function(v, i, a) { return

How come there is a difference between Chrome and Firefox here?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 05:57:44
问题 I am using a recursive function based around for(.. in ..) and hasOwnProperty to clone objects, which works fine in IE and FF... but not Chrome. When iterating over members of an object using for(... in ...) Firefox and Chrome gives different results for hasOwnProperty if the object is a DOM object. Typing the following into the Chrome console vs. the console in Firebug(FF) gives different results: var t = document.createElement("table"); var tr = t.insertRow(-1); for(var p in tr) if(tr

JavaScript: Is a member defined?

徘徊边缘 提交于 2019-12-04 15:48:41
问题 It seems to me that there are four different ways I can determine whether a given object (e.g. foo ) has a given property (e.g. bar ) defined: if (foo.hasOwnProperty(bar)) { if ('bar' in foo) { if (typeof foo.bar !== 'undefined') { if (foo.bar === undefined) { To determine if there is a property named " bar " in the object foo , are all three of those statements equivalent? Are there any sublte semantics I don't know that makes any of these three statements different? 回答1: No they are totally

How come there is a difference between Chrome and Firefox here?

寵の児 提交于 2019-12-04 11:18:01
I am using a recursive function based around for(.. in ..) and hasOwnProperty to clone objects, which works fine in IE and FF... but not Chrome. When iterating over members of an object using for(... in ...) Firefox and Chrome gives different results for hasOwnProperty if the object is a DOM object. Typing the following into the Chrome console vs. the console in Firebug(FF) gives different results: var t = document.createElement("table"); var tr = t.insertRow(-1); for(var p in tr) if(tr.hasOwnProperty(p)) console.log(p); Firefox output: constructor addEventListener Chrome output: clientLeft

Benefit of using Object.hasOwnProperty vs testing if Property is undefined

泪湿孤枕 提交于 2019-12-03 16:32:07
问题 Since hasOwnProperty has some caveats and quirks (window / extensive use in ie8 issues / etc). I was wondering if there is any reason to even use it , and if simply testing if a property is undefined is better justified & more simplistic. For example: var obj = { a : 'here' }; if (obj.hasOwnProperty('a')) { /* do something */ } if (obj.a !== undefined) { /* do something */ } // or maybe (typeof (obj.a) !== 'undefined') Just wondering if anyone has any good insight on this, I'd prefer to be

详解Object.create(null)

守給你的承諾、 提交于 2019-12-03 11:53:51
在Vue和Vuex的源码中,作者都使用了 Object.create(null) 来初始化一个新对象。为什么不用更简洁的 {} 呢? 在 SegmentFault 和 Stack Overflow 等开发者社区中也有很多人展开了讨论,在这里总结成文,温故知新。 Object.create()的定义 照搬一下MDN上的定义: Object.create(proto,[propertiesObject]) proto:新创建对象的原型对象 propertiesObject:可选。要添加到新对象的 可枚举 (新添加的属性是其自身的属性,而不是其原型链上的属性)的属性。 举个例子(恶改了一下MDN的官方例子,看懂的点赞): const car = { isSportsCar: false, introduction: function () { console.log(`Hi girl, this is a ${this.name}. Do you like to have a drink with me ? ${this.isSportsCar}`); } }; const porsche = Object.create(car,{ //color成为porsche的数据属性 //颜色不喜欢,可以改色或贴膜,所以可修改 color:{ writable:true,