[英]Why dot notation failed in my property access?
I'm learning from the book: Javascript the good parts. 我正在从这本书中学习:Javascript的优点。 And I came across the following code augmenting function definitions.
我遇到了以下代码增强功能定义。
Function.prototype.method = function(name, func){
this.prototype[name] = func;
return this;
};
However, if I replace this.prototype[name]
with this.prototype.name
there is an error from Firebug, and I was wondering where is the mistake? 不过,如果我更换
this.prototype[name]
与this.prototype.name
有来自Firebug的错误,我想知道哪里是错误的? Thank you for your help in advance. 提前谢谢你的帮助。
this.prototype.name
等效于this.prototype["name"]
而不是this.prototype[name]
。
you are trying to access the property name
from this.prototype
, where name is a variable. 您正在尝试从
this.prototype
访问属性name
,其中name是变量。 If you used dot notation it would try to look up the literal string 'name'
as a property of the Function.prototype
and of course cant find it. 如果使用点表示法,它将尝试查找文字字符串
'name'
作为Function.prototype
的属性,但是当然找不到它。 Use [name]
if the property name is a variable. 如果属性名称是变量,请使用
[name]
。
In the dot case, name is treated literally, and it is not defined in your case. 在点情况下,名称将按字面意义处理,并且在您的情况下未定义。 On the contrary, the name in the brackets are treated as a reference to a string Object.
相反,方括号中的名称被视为对字符串Object的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.