我读了一篇关于namespace in JavaScript的各种方法的文章。我非常喜欢最后一个的外观,但JSLint不这么认为。我知道JSLint可能过于热心,但有没有办法让这种技术很好玩呢?
var Something = {};
(function () {
"use strict";
this.helloWorld = function () {
var greeting = "Hello World!";
};
}.apply(Something));
答案 0 :(得分:3)
请改为尝试:
var Something = {};
(function (something) {
"use strict";
something.helloWorld = function () {
var greeting = "Hello World!";
};
}(Something));
答案 1 :(得分:-2)
尝试使用call
代替apply
。 apply
定义了两个参数(其中第二个是参数数组),call
实际上只需要一个参数(命名空间),并且包含可选参数列表。