Javascript 如何处理自动执行匿名函数中的对象?
Q1-我有Javascript 如何处理自动执行匿名函数中的对象?,javascript,function,module,anonymous-function,self-executing-function,Javascript,Function,Module,Anonymous Function,Self Executing Function,Q1-我有 (function (document,window) { var shelf = window.shelf = function (foo) { var init = function () { console.log("in init" + foo); }; alert("in shelf.js "+ foo + "type" + typeof init); }; })(document, window); 我想在样式中调用HTML
(function (document,window) {
var shelf = window.shelf = function (foo) {
var init = function () {
console.log("in init" + foo);
};
alert("in shelf.js "+ foo + "type" + typeof init);
};
})(document, window);
我想在样式中调用HTML页面中shelf中的init函数
var api=shelf("1234");
api.init();
或
我是如何让它工作的?我在
,
,
,
我需要文档和窗口对象,因为我将使用它向html页面动态添加组件
问题2-这是更好的方法还是我应该使用其他方法来确保模块化+重用?在您的代码中,
init
不能从外部调用。我相信你在寻找这样的东西:
(function (document,window) {
var shelf = window.shelf = function (foo) {
this.init = function () {
console.log("in init" + foo);
};
alert("in shelf.js "+ foo + "type" + typeof this.init);
};
})(document, window);
var api = new shelf("1234");
api.init();
为了进一步理解模块化,我推荐以下链接(来自同一本书):,
(function (document,window) {
var shelf = window.shelf = function (foo) {
this.init = function () {
console.log("in init" + foo);
};
alert("in shelf.js "+ foo + "type" + typeof this.init);
};
})(document, window);
var api = new shelf("1234");
api.init();