admin管理员组文章数量:1024924
I've been seeing a familiar pattern in JavaScript/jQuery plugin development (Modernizr uses this first example), by which the functions are nicely wrapped in an IIFE and are globally defined on the window object. I am asking is if it's a good idea to make your plugins/scripts accessible globally by doing the following:
window.myPluginName = (function (window, document, undefined) {
// Plugin logic
})(window, document);
I ask this because some scripts/plugins need accessing in the DOM where they're called, or elsewhere in a document/file. Inside the plugins I also see this sometimes:
(function (window, document, undefined) {
window.myPluginName = function () {
// Plugin logic
}
})(window, document);
And even this:
(function (window, document, undefined) {
var myPluginName = function () {
// Plugin logic
}
window.myPluginName = myPluginName;
})(window, document);
I am really intrigued as to the differences behind the setups, and if someone with more experience than I would be kind enough to provide some advice on which is best to use for development. Thank you!
I've been seeing a familiar pattern in JavaScript/jQuery plugin development (Modernizr uses this first example), by which the functions are nicely wrapped in an IIFE and are globally defined on the window object. I am asking is if it's a good idea to make your plugins/scripts accessible globally by doing the following:
window.myPluginName = (function (window, document, undefined) {
// Plugin logic
})(window, document);
I ask this because some scripts/plugins need accessing in the DOM where they're called, or elsewhere in a document/file. Inside the plugins I also see this sometimes:
(function (window, document, undefined) {
window.myPluginName = function () {
// Plugin logic
}
})(window, document);
And even this:
(function (window, document, undefined) {
var myPluginName = function () {
// Plugin logic
}
window.myPluginName = myPluginName;
})(window, document);
I am really intrigued as to the differences behind the setups, and if someone with more experience than I would be kind enough to provide some advice on which is best to use for development. Thank you!
Share Improve this question asked Aug 12, 2013 at 11:06 Stephen JenkinsStephen Jenkins 1,8363 gold badges26 silver badges40 bronze badges 1- This question ends up a nice, concise summary of the solutions. – SexxLuthor Commented Feb 10, 2018 at 6:19
1 Answer
Reset to default 2Short answer: Use the one that makes the most sense to you.
Longer answer: The only difference is where the assignment takes place, but functionality-wise they are all equivalent. None of them has any advantage or disadvantage over the other.
Note: As it stands, the last two wouldn't work because window
is undefined inside the function. But I assume that you'd actually pass the window
object as argument to the IIFE.
I've been seeing a familiar pattern in JavaScript/jQuery plugin development (Modernizr uses this first example), by which the functions are nicely wrapped in an IIFE and are globally defined on the window object. I am asking is if it's a good idea to make your plugins/scripts accessible globally by doing the following:
window.myPluginName = (function (window, document, undefined) {
// Plugin logic
})(window, document);
I ask this because some scripts/plugins need accessing in the DOM where they're called, or elsewhere in a document/file. Inside the plugins I also see this sometimes:
(function (window, document, undefined) {
window.myPluginName = function () {
// Plugin logic
}
})(window, document);
And even this:
(function (window, document, undefined) {
var myPluginName = function () {
// Plugin logic
}
window.myPluginName = myPluginName;
})(window, document);
I am really intrigued as to the differences behind the setups, and if someone with more experience than I would be kind enough to provide some advice on which is best to use for development. Thank you!
I've been seeing a familiar pattern in JavaScript/jQuery plugin development (Modernizr uses this first example), by which the functions are nicely wrapped in an IIFE and are globally defined on the window object. I am asking is if it's a good idea to make your plugins/scripts accessible globally by doing the following:
window.myPluginName = (function (window, document, undefined) {
// Plugin logic
})(window, document);
I ask this because some scripts/plugins need accessing in the DOM where they're called, or elsewhere in a document/file. Inside the plugins I also see this sometimes:
(function (window, document, undefined) {
window.myPluginName = function () {
// Plugin logic
}
})(window, document);
And even this:
(function (window, document, undefined) {
var myPluginName = function () {
// Plugin logic
}
window.myPluginName = myPluginName;
})(window, document);
I am really intrigued as to the differences behind the setups, and if someone with more experience than I would be kind enough to provide some advice on which is best to use for development. Thank you!
Share Improve this question asked Aug 12, 2013 at 11:06 Stephen JenkinsStephen Jenkins 1,8363 gold badges26 silver badges40 bronze badges 1- This question ends up a nice, concise summary of the solutions. – SexxLuthor Commented Feb 10, 2018 at 6:19
1 Answer
Reset to default 2Short answer: Use the one that makes the most sense to you.
Longer answer: The only difference is where the assignment takes place, but functionality-wise they are all equivalent. None of them has any advantage or disadvantage over the other.
Note: As it stands, the last two wouldn't work because window
is undefined inside the function. But I assume that you'd actually pass the window
object as argument to the IIFE.
本文标签: javascriptBest method for adding your script to the window objectStack Overflow
版权声明:本文标题:javascript - Best method for adding your script to the window object? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745505154a2153571.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论