本文介绍了Javascript expando对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javascripts中的expando对象是什么?

What are expando objects in javascripts?

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

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

推荐答案

嗯,在javascript中,任何对象都是一个expando对象。正如文章所述,这意味着每当您尝试访问属性时,它将自动生成。

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

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

当您指定 myProp 一个值时,属性 myProp 是动态创建的,尽管它之前不存在。在许多其他语言中,例如C#,这通常是不可能的(实际上C#也刚刚启用了expando对象支持,但除此之外)。要在C#中访问普通类中的属性,您需要在类中指定它确实具有此属性。

The moment you assign myProp a value, the property myProp is 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.

不非常正确。请参阅下面的npup评论以获得澄清。

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

这篇关于Javascript expando对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 02:33