又来了一个小白问题:看到是这样写一个jQuery代码:jQuery的块风格
var PRINCIPAL = {};
PRINCIPAL.Function = function($element) {
var self = this;
/*more vars*/
this.init = function() {
self.initialFunction();
};
this.initialFunction = function() {
/*code here*/
};
this.secondOne = function() {
/*code here*/
};
this.init();
};
/*associative array below??*/
PRINCIPAL.array = {
CONFIGURATION: 10,
CONFIGURATION2: 200,
init: function($element) {
/*code here*/
},
firstFunction: function() {
/*code here*/
},
secondOne: function() {
/*code here*/
},
thirdOne: function() {
/*code here*/
}
};
/*and at the end it stars all functions inside the blocks*/
$(function() {
PRINCIPAL.array.init($('#element'));
});
好了,现在:有很多阵列像上面的,还有一些其他的(?)函数也是如此,所有的代码都被分块。我不明白,这是一种编程风格还是什么?
在javascript中,'{}'表示对象不是数组。所有'function(){}'都是匿名函数,这些函数用于将函数存储在稍后要执行的变量中。 – sgroves
重新提出你的问题“下面的关联数组?”是的,有点。有些人(主要来自PHP)将对象称为“关联数组”,但在JavaScript中,正常术语仅仅是“对象”或“映射”或(很少)“字典”。 –