Javascript 扩展对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2506005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:39:58  来源:igfitidea点击:

Javascript expando objects

javascriptexpando

提问by xyz

What are expando objects in javascripts?

javascript 中的 expando 对象是什么?

For what purpose we need this ? Any complete example will be appreciated

我们需要这个的目的是什么?任何完整的例子将不胜感激

I found 1 article here Javascript: The red-headed stepchild of web development

我在这里找到了 1 篇文章Javascript: The red-headed stepchild of web development

采纳答案by dxh

Well, in javascript, any object is an expando object. What it means is, as the article covers, that whenever you try to access a property1it will automatically be created.

好吧,在 javascript 中,任何对象都是 expando 对象。这意味着,正如文章所述,每当您尝试访问属性1 时,它将自动创建。

var myObj = {}; // completely empty object
myObj.myProp = 'value';

The moment you assign myPropa value, the property myPropis dynamically created, eventhough it didn't exist before. In a lot of other languages, such as C#, this is not normally possible (actually C# has just enabled expando object support as well, but that's besides the point). To access a property in a normal class in C#, you need to specify in the class that it does indeed have this property.

在您分配myProp值的那一刻,该属性将myProp被动态创建,即使它之前不存在。在许多其他语言中,例如 C#,这通常是不可能的(实际上 C# 也刚刚启用了 expando 对象支持,但这不是重点)。要在 C# 中访问普通类中的属性,您需要在类中指定它确实具有此属性。

1Not quite correct. See npup's comment below for clarification.

1不太正确。请参阅下面的 npup 评论以进行澄清。

回答by Abhijit

Everything except primitive types(string, number,boolean) are objects and support Key:values structure. properties(keys) can be accessed and set using the dot notation as well as the square brackets.

除了原始类型(字符串、数字、布尔值)之外的所有内容都是对象并支持 Key:values 结构。可以使用点符号和方括号访问和设置属性(键)。

var myObj = {};   
myObj.myProp1 = 'value1'; //works, an expando property   
myObj[myProp2] = 'value2'; // doesn't work, myProp2 is an undefined name.
myObj['myProp2'] = 'value2'; // works  , an expando property   
myObj[2010]= 'value'; //note the key is number, still works, an expando property??   
myObj.2010 = 'value'; // FAILS. to use dot notation, key must be a string

回答by Freeman

JavaScript turns elements with specific IDs of names into expandos of the returned DOM object. It is explained here.

JavaScript 将具有特定名称 ID 的元素转换为返回的 DOM 对象的扩展。它解释here

回答by Quentin

An article written in 2007 that uses document.all (as the onlyway to access elements)? That's a big red flag.

2007 年写的一篇使用 document.all(作为访问元素的唯一方式)的文章?这是一个很大的红旗。

It is just dressing up "You can add properties to an object" with some buzzwords.

它只是用一些流行语来修饰“您可以向对象添加属性”。

We need to be able to do this because otherwise we wouldn't be able to store data, and that would make JavaScript a pretty useless language.

我们需要能够做到这一点,否则我们将无法存储数据,这将使 JavaScript 成为一种非常无用的语言。

(Everything is an array? No it isn't. And it iterates over an object without a hasOwnProperty wrapper. That isn't safe. Just keep away from the article, it is worse than useless)

(一切都是一个数组?不,它不是。它在没有 hasOwnProperty 包装器的情况下迭代对象。这不安全。远离文章,它比无用更糟糕)