使用原型时停止 JavaScript 中的枚举

发布于 2024-12-27 11:23:31 字数 874 浏览 6 评论 0原文

我目前正在尝试在 JavaScript 中使用原型。

为了对此进行实验,我编写了一个函数,该函数可以有效地工作,允许您将 where 子句放到数组上:

Array.prototype.where=(function(){
  var tmpArr=[],
      success;

  for (var x in this){
    var success=true;
    for (var i in arguments){
      if (this[x][arguments[i][0]]!=arguments[i][1]){
        success=false;
        break;
      }
    }

    if (success==true){
      tmpArr.push(this[x]);
    }
  }
  return tmpArr;
});

一个示例用法是:

arrayName.where([0, 'Fred'], [1, 'Bloggs']);

为了进行测试,这工作得很好。唯一的问题是,如果您随后运行,

for (var x in someArrayHere){
  console.log(someArrayHere[x]);
}

您将获得数组的输出,但带有代表您原型化的函数的记录。

据我所知,这是通过将函数设置为不可枚举来排序的,但我找不到任何解释如何停止它的文章。

我该怎么办呢?或者我每次都必须执行以下操作?

for (var x in someArray){
  if (typeof tSch[x]!="object"){
  }
}

I'm currently trying to get my head around using prototype in JavaScript.

To experiment with this, I've written a function that effectively works as allowing you to put a where clause onto arrays:

Array.prototype.where=(function(){
  var tmpArr=[],
      success;

  for (var x in this){
    var success=true;
    for (var i in arguments){
      if (this[x][arguments[i][0]]!=arguments[i][1]){
        success=false;
        break;
      }
    }

    if (success==true){
      tmpArr.push(this[x]);
    }
  }
  return tmpArr;
});

An example use would be:

arrayName.where([0, 'Fred'], [1, 'Bloggs']);

For the sake of a test, this works pretty well. The only problem is if you were then to run

for (var x in someArrayHere){
  console.log(someArrayHere[x]);
}

You get the output the array, but with a record representing the function you've prototyped.

As far as I can work out, this is sorted by setting the function as non-enumerable, but I can't find any articles explaining how to stop it.

How would I go about it? Or would I have to do the below each time?

for (var x in someArray){
  if (typeof tSch[x]!="object"){
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘和柠 2025-01-03 11:23:31

for...in 构造枚举对象的所有属性(以及沿原型链继承的属性)。当您将它与数组一起使用时,它将迭代 Array.prototype 的所有属性以及数组的元素。

使用普通的 for 循环来防止这种情况:

for(var x = 0; x < this.length; x++) {
    //Do stuff
}

The for...in construct enumerates all properties of an object (and those inherited down the prototype chain). When you use it with an array, it will iterate over all properties of Array.prototype, as well as the elements of the array.

Use a normal for loop to prevent this:

for(var x = 0; x < this.length; x++) {
    //Do stuff
}
昔日梦未散 2025-01-03 11:23:31

一般来说,使用 for ... in 循环来迭代数组是不好的做法。

但是,一个简单的修复方法是使用 Object.hasOwnProperty

for (var x in someArray) {
  if (someArray.hasOwnProperty(x)) {
    //...
  }
}

这是一种比循环更强大的方法。

In general, using a for ... in loop to iterate through an array is bad practice.

However, a simple fix would be to use Object.hasOwnProperty:

for (var x in someArray) {
  if (someArray.hasOwnProperty(x)) {
    //...
  }
}

This is a more robust approach than your loop.

沦落红尘 2025-01-03 11:23:31

一般来说,您不应该弄乱本机对象的原型。这会导致问题 - 特别是与外部代码/库结合使用时。

相反,您应该将其设为对数组参数进行操作的函数(例如function arrayWhere(arr))。

如果您想坚持扩展本机原型,则应该使用 迭代数组forEach 方法(较新的浏览器,可能会添加后备,如 MDN 中所述),或对象的 hasOwnProperty 方法,如 在此示例中

In general, you should not mess with the prototypes of native Objects. This leads to problems - especially in combination with extern code/libraries.

Instead, you should make it a function that operates on an array parameter (for instance function arrayWhere(arr)).

If you want to stick to extending native prototypes, you should iterate over arrays with the forEach method (newer browsers, maybe add a fallback as described at the MDN), or the hasOwnProperty method of objects, as described in this example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文