如何在Array.prototype和Object.prototype的javascript中定义方法,以便它不出现在循环中 [英] How to define method in javascript on Array.prototype and Object.prototype so that it doesn't appear in for in loop

查看:94
本文介绍了如何在Array.prototype和Object.prototype的javascript中定义方法,以便它不出现在循环中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Array.prototype和Object.prototype上定义helper方法。我目前的计划是做一些事情:

  Array.prototype.find = function(testFun){
//在数组
}中查找元素的代码;

所以我可以这样做:

  var arr = [1,2,3]; 
var found = arr.find(function(el){return el> 2;});

它可以正常工作,但如果我在中为循环的方法显示为值:

  for(var arr arr){console.log(prop ); } 
//输出:
// 1
// 2
// 3
//找到

这会使依赖的其他人在中显示值(特别是在对象上) 。 JavaScript的更高版本具有内置到数组中的.map和.filter函数,但在循环中不会显示。我怎样才能创建更多像循环中中不会显示的方法?

解决方案

这很简单:不要对数组使用 for-in循环。指责所有其他人 - 这是一个很好的片段,以在开发过程中告诉他们。



当然,如果在泛型函数中进行枚举并且不知道他是否获取了数组,纯文本对象或带有自定义原型的对象,则可以使用 hasOwnProperty 像这样:
$ b $ pre $ for(var在anyObj中)
if(Object.prototype.hasOwnProperty.call(anyObj, prop))
//做某事

注意显式使用 Object.prototype 来获取函数 - 可能有对象覆盖它(特别是在数据映射中,该值甚至可能不是函数),不支持它或没有插入的物体从Object.prototype完全依赖。另请参阅此处



然而,只有脚本作者意识到问题会过滤他所有的for-in-loops - 有些只会这样做,因为获得推荐 - 并且它大部分都是错误的,他应该使用for-loop数组迭代。但是,我们的问题是那些不知道它的作者。

一个有趣的,但Mozilla的方法将覆盖数组枚举的行为通过 __ iterate __ ,因为在这里展示



幸运的是,EcmaScript 5.1允许我们设置属性为不可枚举。当然,这在旧版浏览器中不受支持,但为什么呢?无论如何,我们需要使用 es5-shims 来处理所有酷炫的高阶数组内容: - )使用 defineProperty 像这样:

$ p $ Object.defineProperty(Array.prototype,find,{
enumerable:false,
可写:true,
value:function(testFun){
//在元素中查找元素的代码
}
});


I want to define helper methods on the Array.prototype and Object.prototype. My current plan is to do something like:

Array.prototype.find = function(testFun) {
   // code to find element in array
};

So that I can do this:

var arr = [1, 2, 3];
var found = arr.find(function(el) { return el > 2; });

It works fine but if I loop over the array in a for in loop the methods appear as values:

for (var prop in arr) { console.log(prop); }
// prints out:
// 1
// 2
// 3
// find

This will screw up anybody else relying on the for in to just show values (especially on Objects). The later versions of javascript have .map and .filter functions built into arrays but those don't show up on for in loops. How can I create more methods like that which won't show up in a for in loop?

解决方案

It's quite easy: Don't use for-in loops with Arrays. Blame everybody else who does so - here is a nice snippet to tell them during development.

Of course, if one does an enumeration in a generic function and doesn't know whether he gets an array, a plain object or an object with a custom prototype, you can use hasOwnProperty like this:

for (var prop in anyObj )
    if (Object.prototype.hasOwnProperty.call(anyObj, prop))
        // do something

Notice the explicit use of Object.prototype to get the function - there might be objects that overwrite it (especially in data-maps, the value might not even be a function), objects that do not support it or objects that do not inherit from Object.prototype at all. See also here.

Yet, only a script author who is aware of the problem would filter all his for-in-loops - and some only do it because it gets recommended - and does it mostly wrong, he should have used a for-loop array iteration instead. But our problem are those authors who do not know of it.

An interesting, but Mozilla-only approach would be overwriting the behavior of enumerations on arrays via __iterate__, as demonstrated here.

Fortunately, EcmaScript 5.1 allows us setting properties to be non-enumerable. Of course, this is not supported in older browsers, but why bother? We'd need to use es5-shims anyway for all the cool higher-order array stuff :-) Use defineProperty like this:

Object.defineProperty(Array.prototype, "find", {
    enumerable: false,
    writable: true,
    value: function(testFun) {
        // code to find element in array
    }
});

这篇关于如何在Array.prototype和Object.prototype的javascript中定义方法,以便它不出现在循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆