什么是正确的JavaScript对象格式?

[英]What is the proper JavaScript object format?


I made an exercise about ng-repeat with AngularJS 1.x , everything goes well.

我用AngularJS 1.x做了关于ng-repeat的练习,一切顺利。

JavaScript:

var app = angular.module('myModule', []);

app.controller('myController', function() {
  this.dishes = [
    {
      'name': 'CD',
      'drink': 'wine',
      'color': 'red'
    },
    {
      name: 'vagetable',
      drink: 'water',
      color: 'blue'
    },
    {
      'name': 'meat',
      'drink': 'coffee',
      'color': 'brown'
    }
  ];
});

HTML:

<html lang="en" ng-app="myModule">

<head></head>

<body ng-controller="myController as myCtrl">
  <div> {{myController.dishes}} </div>
  <ul>
    <li ng-repeat="dish in myCtrl.dishes">
      <p> {{dish.name}} is my name.</p>
      <p> {{dish.drink}} is something you can drink.</p>
      <p> {{dish.color}} is the color I wear.</p>
      <hr>
    </li>
    <p> {{myCtrl.theModelContent}}</p>
    <input type='text' ng-model="myCtrl.theModelContent">
  </ul>
</body>

</html>

I noticed that 'name': 'CD' works fine as well as name: 'vegetable'. What's the difference between these two?

我注意到'名字':'CD'和名字一样好:'蔬菜'。这两者有什么区别?

1 个解决方案

#1


0  

JSON is a wire format where everything is a string, thus the usual call of JSON.stringify for your JavaScript object literal. Whereas JavaScript object literals can have string or non-string keys. The short answer is having a string key in a JavaScript object literal is optional, while it is required in the JSON-formatted version of that object literal.

JSON是一种有线格式,其中所有内容都是字符串,因此通常调用JSON.stringify作为JavaScript对象文字。 JavaScript对象文字可以包含字符串或非字符串键。简短的回答是JavaScript对象文字中的字符串键是可选的,而在该对象文字的JSON格式版本中需要它。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/09/10/30f8cb12cdc6281d9f28cf6cb514d8b7.html



 
  © 2014-2022 ITdaan.com 联系我们: