javascript - 使用 = 和 : in javascript 有什么区别

标签 javascript

 var app = new Vue({
 el: '#main',
 template: $("#products-template").text(),
 data: {
loading: true,
products: []
},
ready: function() {
var self = this;
$.getJSON(url, function(fbresults){
self.products = fbresults.data;
self.loading  = false;
});
}
});

var app = new Vue({
el= '#main',
template= $("#products-template").text(),
data= {
loading= true,
products= []
},
ready= function() {
var self = this;
$.getJSON(url, function(fbresults){
self.products = fbresults.data;
self.loading  = false;
});
}
});

上面的代码片段中使用了‘=’和‘:’,那么什么时候需要使用=,什么时候需要使用:,主要是做什么用的

最佳答案

这里的冒号在声明对象文字的属性时使用:

{
    key: value,
    key2: value2
}

等于运算符将值分配给变量或表达式:

foo = 5;
obj.key = value;

在您的示例中,冒号定义传递到 Vue 的对象的属性。如果使用正确的缩进,效果会更加明显:

var app = new Vue({
    el: '#main',
    template: $("#products-template").text(),
    data: {
        loading: true,
        products: []
    },
    ready: function() {
        var self = this;
        $.getJSON(url, function(fbresults){
            self.products = fbresults.data;
            self.loading  = false;
        });
    }
});

关于javascript - 使用 = 和 : in javascript 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25553578/

相关文章:

javascript - 如何测试 vue.js v-select 组件的值

javascript - 如何在 Angular 2 中重用服务调用

javascript - jquery $.each 与 for 循环(具有异步内容)

javascript - onunload 无法与 Safari 中的 onbeforeunload 结合使用

javascript - 为什么表格有效?

javascript - 如何从对象中的数组中弹出和取消移位元素?

javascript - 匹配最后一个冒号之后的字符串

javascript - $(this) 不同于 $.each 循环上下文中的 'element' 参数

javascript - 未处理的PromiseRejection警告: MongooseServerSelectionError

javascript - Swig 模板 : How to check if value exists in array?