[英]Why the parameter can't be passed properly when function called?
var collection = {
"5439": {
"album": "ABBA Gold"
}
};
function updateRecords(id, prop, value) {
if(prop !== "tracks" && value !== ""){
collection[id].prop=value;
}
return collection;
}
updateRecords(5439, "artist", "ABBA");
why the result is Object { album="ABBA Gold", prop="ABBA"}
,not Object { album="ABBA Gold",artist="ABBA"}
? 为什么结果是
Object { album="ABBA Gold", prop="ABBA"}
,而不是Object { album="ABBA Gold",artist="ABBA"}
?
When to parse collection[id].prop=value;
何时解析
collection[id].prop=value;
,the value of prop is artist,the value of id is 5439,so collection[id].prop=value;
,道具的值为艺术家,id的值为5439,因此
collection[id].prop=value;
should parsed into collection[5439].artist="ABBA";
应该解析为
collection[5439].artist="ABBA";
,why not? ,为什么不?
Use Bracket notation
使用
Bracket notation
An
object
property name can be any valid JavaScript string, or anything that can be converted to a string, including the empty string.object
属性名称可以是任何有效的JavaScript字符串,也可以是任何可以转换为字符串的内容,包括空字符串。 However, any property name that is not a valid JavaScript identifier (for example, a property name that has a space or a hyphen, or that starts with a number) can only be accessed using the square bracket notation.但是,任何不是有效JavaScript标识符的属性名称(例如,具有空格或连字符或以数字开头的属性名称)都只能使用方括号表示法进行访问。 This notation is also very useful when property names are to be dynamically determined (when the property name is not determined until runtime).
当要动态确定属性名称时(当直到运行时才确定属性名称时), 该符号也非常有用 。
collection[id][prop] = value;
instead of, 代替,
collection[id].prop=value;
change the line collection[id].prop=value;
更改行
collection[id].prop=value;
to collection[id][prop]=value;
collection[id][prop]=value;
var collection = {
"5439": {
"album": "ABBA Gold"
}
};
function updateRecords(id, prop, value) {
if (prop !== "tracks" && value !== "") {
collection[id][prop] = value;
}
return collection;
}
updateRecords(5439, "artist", "ABBA");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.