简体   繁体   English

这是Vue.js中的javascript对象吗?

[英]Is this a javascript object in Vue.js?

I see the following format of code in Vue.js tutorials a lot and was wondering if it is a javascript object. 我在Vue.js教程中经常看到以下代码格式,并且想知道它是否是javascript对象。 I'm checking w3schoosl now and it seems javascript objects are defined by a variable instead of having the straight return command followed by the parenthesis. 我现在正在检查w3schoosl,看来javascript对象是由变量定义的,而不是在括号后加直接返回命令。 Could someone guide me in the correct direction? 有人可以指导我正确的方向吗? Thanks! 谢谢! :) I'm trying to learn the code one by one and sometimes am confused whether something belongs to the framework I'm using or plain javascript. :)我试图一一学习代码,有时会困惑是属于我使用的框架还是纯JavaScript。 :) :)

return {
        messages: [
          {
            message: 'Hey!',
            user: 'John Doe'
          },
          {
            message: 'Hello!',
            user: 'Jane Jennings'
          }
        ]
      }

This is a plain javascript object. 这是一个普通的javascript对象。 Returning the way that you did or assigning to a variable produces the same result, it still a javascript object. 返回您所做的方式或将其分配给变量会产生相同的结果,但它仍然是一个javascript对象。 Directly returning the object is just a shortcut and reduces the amount of code, producing the same results. 直接返回对象只是一种捷径,它减少了代码量,产生了相同的结果。

Vue is written using javascript so it uses javascript objects too. Vue使用javascript编写,因此它也使用javascript对象。

You are returning a javascript object with a property called messages which, in turn, is an array containing several objects, each separated by a comma. 您将返回一个具有名为messages的属性的javascript对象,该属性又是一个包含多个对象的数组,每个对象之间都用逗号分隔。

You can tell it's an object because of the { ... } that encapsulates a group of properties. 您可以说它是一个对象,因为{ ... }封装了一组属性。

// object
{
    property: 'value'
}

You can tell that the message property is an array because of the [ ... ] that encapsulates a list of items, separate by commas. 您可以说出message属性是一个数组,因为[ ... ]封装了一个项目列表,以逗号分隔。

message: [
    {}, // first object, notice the comma
    {}  // second object
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM