转:http://www.cnblogs.com/sniper007/archive/2012/11/30/2796029.html
javascript对象的expandos
【正文】expando 是 expandable object 的缩写,表示可扩展的对象。expando property 表示可扩展对象的动态属性,运行时添加的。expando 可以直接表示 expando property.
JavaScript 中的所有对象均支持“expando”属性,即可在运行时动态添加和删除的属性。 这些属性可采用包括数字在内任何名称。 如果属性的名称是简单的标识符,则可在对象名称与句点之后加入该属性,如:
1
var myObj =
new Object();
2
3 // Add two expando properties, 'name' and 'age'
4 myObj.name = "Fred";
5 myObj.age = 42;
2
3 // Add two expando properties, 'name' and 'age'
4 myObj.name = "Fred";
5 myObj.age = 42;
如果属性的名称不是简单的标识符,或在编写脚本时不知道该属性,则可在方括号内使用任意表达式作为属性的索引。JavaScript 中所有 expando 属性的名称先转换为字符串,然后再添加到对象后。
var myObj =
new Object();
// Add two expando properties that cannot be written in the
// object.property syntax.
// The first contains invalid characters (spaces), so must be
// written inside square brackets.
myObj["not a valid identifier"] = "This is the property value";
// The second expando name is a number, so it also must
// be placed inside square brackets
myObj[100] = "100";
// Add two expando properties that cannot be written in the
// object.property syntax.
// The first contains invalid characters (spaces), so must be
// written inside square brackets.
myObj["not a valid identifier"] = "This is the property value";
// The second expando name is a number, so it also must
// be placed inside square brackets
myObj[100] = "100";
参考文档:
http://msdn.microsoft.com/zh-cn/library/89t1khd2%28v=VS.94%29.aspx
http://blogread.cn/it/article.php?id=2419