2

I'm pretty new to plain old JavaScript and to JavaScript frameworks (such as Backbone.js, RequireJS, ...). As I was reading and trying to understand some JavaScript files that I got from a project at work (based on JQuery, Backbone and Require), I've encountered some variable declarations such as:

var myVariable = {}, itemList;

Could someone explain to me what the "{}" is?

PS: might be a silly question but it's definitely not that easy Googling for "{}" as keyword...

Thanks in advance.

4

4 回答 4

4

{}只是定义集合或对象的javascript方式。

在这个例子中,它填充了一个对象字面量

var apple = {
    type: "macintosh",
    color: "red",
    getInfo: function () {
        return this.color + ' ' + this.type + ' apple';
    }
}

var featurelessApple = {};
于 2012-07-18T10:31:01.750 回答
2

myVariable是一个对象字面量或泛型object

为了将来参考,您可以使用浏览器中的控制台轻松查看此内容。在 Chrome 中

var myVariable = {}
console.log(myVariable);

这将打印出整个对象。在这个

Object {}
于 2012-07-18T10:32:26.083 回答
2

它是一个空的 Javascript 对象字面量(创建对象的简写方式)

var myVariable = {};

类似于

var myVariable = new Object();

两个表达式都将创建一个空对象。

于 2012-07-18T10:33:24.763 回答
1

那是一个空的对象字面量

对象字面量包含在花括号中的零个或多个键/值对。在您的示例中,键/值对为零,因此该对象未定义任何属性。

于 2012-07-18T10:29:24.560 回答