使用Object.create(null)与{}是不好的做法吗? [英] Is it bad practice to use Object.create(null) versus {}?

查看:166
本文介绍了使用Object.create(null)与{}是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更喜欢使用 Object.create(null),这样我就可以将对象用作简单的字典来存储基本信息。如果我需要对象功能,那么我将创建一个普通对象,例如 var obj = {x:3,etc};

I prefer to use Object.create(null), this way I can use the object as a simple dictionary to store basic information. If I need object functionality, then I'd create a normal object, e.g. var obj = {x:3, etc};

这是因为{}并非真正的空白对象因为它链接到Object原型所以在循环中你必须做 hasOwnProperty 来阻止它在原型链周围跳舞并对每个循环进行检查会妨碍性能,尽管它可以忽略不计。

This is because {} isn't truly a blank object as it links to the Object prototype so in loops you have to do hasOwnProperty to stop it dancing around the prototype chain and doing this check on every loop hinders performance, albeit it's negligible.

使用 Object.create(null)而不是 {是不好的做法} ,所以我不必在每个循环中写 hasOwnProperty

Is it bad practice to use Object.create(null) instead of {}, so I don't have to write hasOwnProperty in every loop?

编辑:可能重复,但我不是在询问他们是如何或为何与众不同,而是我有兴趣知道其他人是否使用 Object.create(null)? Javascript中的 SOP 是?

Possible duplicate, but I'm not asking about how or why they're different, instead I'm interested to know if others use Object.create(null)? Is is SOP in Javascript land?

编辑2:我也更喜欢进行单行检查,例如: if(obj [someKey])... ht'向APJ Kalam Sir致敬'。

Edit 2: I also prefer to do one-line checks, e.g. if(obj[someKey])... ht 'Tribute to APJ Kalam Sir'.

推荐答案

据我所知,

{} == Object.create(Object.prototype);

此外,

给我(仅限) me)我觉得 Object.create(null)让你更清楚你的对象不会继承任何东西,即它(纯粹)空图

To me (Only me) I feel like Object.create(null) gives you more clarity that your object is not going to inherit anything i.e its (purely) empty map.

此外,

在这两种情况下,您都可以将对象用作字典。

In both cases you can use object as dictionary.

此外,

它取决于你对你的对象应该做什么。

It depend upto you what you supposed to do with your object.

何时,


  1. {} == Object.create(Object.prototype); //你继承了所有对象属性

  1. {} == Object.create(Object.prototype); // you inherit all object properties

o = Object.create(null); //你不继承任何东西。这是完全空白的对象。

o = Object.create(null); // you do not inherit anything. this is completely blank object.

如果您使用对象作为地图,并使用方法创建对象如上所述,在地图中进行查找时,您必须格外小心。由于继承了Object中的属性和方法,因此您的代码可能会遇到地图中有从未插入的键的情况。

If you are using an object as a map, and you create an object using method 1 above, then you have to be extra careful when doing lookups in the map. Because the properties and methods from Object are inherited, your code may run into a case where there are keys in the map that you never inserted.

例如,如果你对toString进行了查找,你会发现一个函数,即使你从未在那里放过那个值。你可以这样解决:

For example, if you did a lookup on toString, you would find a function, even though you never put that value there. You can work around that like this:

if (Object.prototype.hasOwnProperty.call(object, 'toString')) {
    // we actually inserted a 'toString' key into object
}

请注意,您不能只执行object.hasOwnProperty('toString'),因为您可能已将一个键hasOwnProperty插入到对象中,因此我们强制它使用Object中的实现。

Note that you can't just do object.hasOwnProperty('toString') because you may have inserted a key "hasOwnProperty" into object, so we force it to use the implementation in Object.

另一方面,如果你使用上面的方法2,那么你不必担心地图中出现的对象。

On the other hand, if you use method 2 above, then you won't have to worry about things from Object appearing in the map.

你无法检查是否存在简单的属性,如下所示:

You can't check for the existence of a property with a simple if like this:

// Unreliable:
if (object[someKey]) {
    // ...
}






SO,


SO,

要将对象用作地图,大多数地球上的人都会使用

To use object as a Map you most of the people on planet use

Object.create(null) //无需担心继承属性的开销。

Object.create(null) // no overhead to worry about inherited properties.

在所有其他情况下,您只需使用,

In all other cases you simply use,

var myObject = {}

MDN - Object.create

MSDN - Object.create

这篇关于使用Object.create(null)与{}是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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