【问题标题】:Javascript getting property valueJavascript获取属性值
【发布时间】:2018-02-05 18:43:01
【问题描述】:

我的问题是:

作品:

console.log(variable[apple]);

variable: {
  apple : "xxx"
}

不工作:

console.log(variable.element[apple]);

variable: {
  element: {
    apple: "xxx"
  }
}

element 不是未定义的。 有什么想法吗?

【问题讨论】:

标签: javascript json node.js


【解决方案1】:
variable: {  //this is a code block labeled "variable"
  element: {  //this is a code block labeled "element"
    apple: "xxx"  //this is an expression "xxx" labeled as "apple"
  }
}

但是您的代码中没有变量。阅读labeled statements

你可能是说

var variable = {  //now this is a variable containing an object
  element: {  //and these are properties of that object
    apple: "xxx"  
  }
}

console.log(variable.element.apple);

【讨论】:

    猜你喜欢
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 2016-03-10
    • 2015-09-21
    • 1970-01-01
    相关资源
    最近更新 更多