这会做到的:)
(function ($) {
// define some methods
var example = {
method1: function() { console.log(1); },
method2: function() { console.log(2); }
};
// run all methods in example
for (var m in example) {
if (example.hasOwnProperty(m) && typeof example[m] === "function") {
example[m]();
}
}
// => 1
// => 2
})(jQuery);
如果你想使用new
诸如
var example = new Example();
// => "A"
// => "B"
你可以做这样的事情
(function($) {
var Example = function() {
this.initializeA();
this.initializeB();
};
Example.prototype.initializeA = function() {
console.log('A');
}
Example.prototype.initializeB = function() {
console.log('B');
};
// init
new Example();
// => "A"
// => "B"
})(jQuery);