简体   繁体   English

为什么在调用函数时无法正确传递参数?

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

相关问题 检查传递特定参数时是否调用函数 - Check if a function is called when a specific parameter is passed 为什么这样传递函数参数不成为键值? - Why doesn't function parameter become key value when passed in as such? 为什么作为函数参数传递的Javascript数组可能会在顺序调用函数时丢失它的内容? - Why a Javascript array passed as function parameter may lose it's content when the function is called sequentially? Function 作为未调用的参数传递 - Function passed as parameter not being called 当消费者调用时,作为上下文值传递的函数无法访问更新的组件状态 - Function passed as context value can't access updated component state when called by a consumer 为什么从编辑器调用时将 null 传递给我的函数? - Why is null passed to my function, when called from the editor? 为什么箭头 function 中的参数可以这样传递? [等候接听] - Why can the parameter in arrow function be passed on like this? [on hold] 为什么可以将参数传递给在 javascript 中没有参数的函数? - Why can argument(s) be passed to a function that has no parameter in javascript? 如何测试 function 是否作为参数传递给 Jest - How to test if function passed as parameter was called in Jest Jasmine:无法测试作为参数传递的函数 - Jasmine: can't test function being passed as parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM