我有一些JavaScript代码,需要像BackboneJS一样重叠到自执行匿名函数中:
(function(root, factory) {
// Set up Backbone appropriately for the environment. Start with AMD.
if (typeof define === 'function' && define.amd) {
define(['underscore', 'jquery', 'exports'], function(_, $, exports) {
// Export global even in AMD case in case this script is loaded with
// others that may still expect a global Backbone.
root.Backbone = factory(root, exports, _, $);
});
// Next for Node.js or CommonJS. jQuery may not be needed as a module.
} else if (typeof exports !== 'undefined') {
var _ = require('underscore');
factory(root, exports, _);
// Finally, as a browser global.
} else {
root.Backbone = factory(root, {}, root._, (root.jQuery || root.Zepto || root.ender || root.$));
}
}(this, function(root, Backbone, _, $) {
最佳答案
答案是肯定的,
如果您想将文件分成几部分,然后在连接后将其放在这样的内部函数中
(function(){
// file 1 content
// file 2 content
})();
您需要grunt.loadNpmTasks('grunt-contrib-concat');
并定义标语和页脚:
concat: {
app: {
options: {
separator: ';\n',
banner: '(function(){\n',
footer: '\n})();'
},
src: [
'src/*.js'
],
dest: 'app.js'
}
},
关于javascript - 是否有grunt插件来将代码包装到BackboneJS中的自执行匿名函数中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26489721/