定义方法
window.define("test", ["dom","window"],
function (dom, window)
{
function testAlert(){alert("aa")}
}
);
调用:
window.require(["test"], function(t){t.testAlert()});
求教,上面的window.define 和window.require是js方法吗?语法怎么用?百度了一圈没搜到。
一段脚本,你是不是少了一些代码,参考一下下面的代码吧。
/* * 使用 nodejs 形式的语法来规整 js */ void function() { var mapping = {}, cache = {}; window.define = function(id, func) { mapping[id] = func }; window.require = function(id) { if (!/\.js$/.test(id)) { id += ".js" } if (cache[id]) { return cache[id] } else { return cache[id] = mapping[id]() } } }(); define("scripts/core/demo.js", function(exports) { exports = {}; exports.start = function() { alert("Hello World !") }; return exports }); require('scripts/core/demo').start();
冠军