简体   繁体   English

为什么点符号在我的媒体资源访问中失败?

[英]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.

相关问题 javascript对象-使用点表示法访问子属性 - javascript Object - access child property using dot notation 使用带点符号的整数键来访问 javascript 对象中的属性 - Using integer keys with dot notation to access property in javascript objects JavaScript 属性访问:点符号与括号? - JavaScript property access: dot notation vs. brackets? 实现一个函数来访问对象的属性,而不是使用点符号或方括号符号来访问属性? - Implementing a function to access an object's property versus accessing the property using dot notation, or bracket notation? 为什么使用点*和*括号访问权限来分配属性? - Why use dot *and* bracket access to assign a property? 为什么通过点表示法的javascript对象属性赋值不会触发为该属性定义的setter? - Why does a javascript object property assignment via the dot notation NOT trigger a setter defined for that property? Angular 2-[对象对象]-使用点表示法访问对象的属性不起作用 - Angular 2 - [Object Object] - Access the property of the object using dot notation does not work 查询猫鼬的嵌套属性(不带点表示法) - Query nested property in Mongoose (without dot notation) 传递带有点符号的属性(有时)以起作用 - Pass property with dot notation(sometimes) to function IE11中的点符号属性访问器 - Dot notation property accessor in IE11
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM