如何调用这个封装在jQuery函数中的JavaScript方法
我正在研究一个JavaScript文件,并在其中看到一些方法被包装在jQuery函数中。有人能帮我怎么调用下面的方法吗?我可以知道它的优点是什么,或者为什么这个方法被包装在一个函数中吗?下面是我的示例JavaScript代码 JQuery/JavaScript如何调用这个封装在jQuery函数中的JavaScript方法,javascript,jquery,Javascript,Jquery,我正在研究一个JavaScript文件,并在其中看到一些方法被包装在jQuery函数中。有人能帮我怎么调用下面的方法吗?我可以知道它的优点是什么,或者为什么这个方法被包装在一个函数中吗?下面是我的示例JavaScript代码 JQuery/JavaScript $(document).ready(function () { //How to invoke "testMethod" method? $(function () { function testMetho
$(document).ready(function () {
//How to invoke "testMethod" method?
$(function () {
function testMethod() {
alert("this is a test method");
}
});
});
正如您所声明的,
testMethod()
是一个本地函数,仅在声明它的函数范围内可用。如果希望它在该范围外可调用,则需要以不同的方式定义它,以便它在更大的范围内可用
一种方法是使其成为一个全局函数:
$(document).ready(function () {
//How to invoke "testMethod" method?
$(function () {
window.testMethod = function() {
alert("this is a test method");
}
});
});
testMethod(); // available globally now
它也可以附加到一个全局名称空间,或者可以在更高的范围内定义,这样也可以解决您的问题。如果没有关于您的具体情况,我们无法建议哪一个是最好的,但是您需要做的主要事情是更改函数的声明方式,以便它在您想要调用它的范围内可用
另外,为什么一个文档就绪函数嵌套在另一个文档就绪函数中?这不提供额外的功能,增加了不必要的复杂性。此外,如果您希望文档就绪处理程序在全局可用,那么实际上没有理由在其内部定义
testMethod()
。正如您所声明的那样,testMethod()
是一个局部函数,并且仅在声明它的函数范围内可用。如果希望它在该范围外可调用,则需要以不同的方式定义它,以便它在更大的范围内可用
一种方法是使其成为一个全局函数:
$(document).ready(function () {
//How to invoke "testMethod" method?
$(function () {
window.testMethod = function() {
alert("this is a test method");
}
});
});
testMethod(); // available globally now
它也可以附加到一个全局名称空间,或者可以在更高的范围内定义,这样也可以解决您的问题。如果没有关于您的具体情况,我们无法建议哪一个是最好的,但是您需要做的主要事情是更改函数的声明方式,以便它在您想要调用它的范围内可用
另外,为什么一个文档就绪函数嵌套在另一个文档就绪函数中?这不提供额外的功能,增加了不必要的复杂性。此外,如果您希望文档就绪处理程序在全局可用,那么实际上没有理由在其内部定义
testMethod()
。您的调用需要在$(函数
中)
这都是关于范围的,您需要将testMethod从$(函数
)中分离出来
您是否可以进一步解释您的要求,以便我们可以提供更好的帮助?您的呼叫需要在
$(函数
中)
这都是关于范围的,您需要将testMethod从$(函数
)中分离出来
您是否可以进一步解释您的要求,以便我们可以提供更好的帮助?进入就绪活动:
$(document).ready(function () {
//How to invoke "testMethod" method?
var testMethod = function () {
alert("this is a test method");
}
// V0.1
testMethod();
// V0.2
$('#some_id').click(testMethod);
});
在其他部分:
myObj = {testMethod: null};
$(document).ready(function () {
//How to invoke "testMethod" method?
myObj.testMethod = function () {
alert("this is a test method");
}
});
// Something else
if( myObj.testMethod ) myObj.testMethod();
准备就绪事件:
$(document).ready(function () {
//How to invoke "testMethod" method?
var testMethod = function () {
alert("this is a test method");
}
// V0.1
testMethod();
// V0.2
$('#some_id').click(testMethod);
});
在其他部分:
myObj = {testMethod: null};
$(document).ready(function () {
//How to invoke "testMethod" method?
myObj.testMethod = function () {
alert("this is a test method");
}
});
// Something else
if( myObj.testMethod ) myObj.testMethod();
除此之外:
$(document).ready(function(){...});
//is the same as
$(function(){...}}
至于你的问题,这里有一些可能的方法:
- 如果该函数是每个人都使用的某个实用函数,那么就让它在某个名称空间中对所有人都可用,比如在这个名为
实用程序的名称空间中:
//Utility module (function(ns){ //declaring someFunction in the Utility namespace //it's available outside the ready handler, but lives in a namespace ns.someFunction = function(){...} }(this.Utility = this.Utility || {})); $(function(){ //here in the ready handler, we use it Utility.someFunction(); });
- 如果它们都生活在
处理程序中,并且希望处理程序中的所有代码都使用它,则在处理程序的最外层声明它,以便所有嵌套作用域都能看到它ready
$(function(){ //declare it in the outermost in the ready handler function someFunction(){...} //so we can use it even in the deepest nesting function nestedSomeFunction(){ someFunction(); } someElement.on('click',function(){ $.get('example.com',function(){ someFunction(); }); }); nestedSomeFunction(); someFunction(); });
$(document).ready(function(){...});
//is the same as
$(function(){...}}
至于你的问题,这里有一些可能的方法:
- 如果该函数是每个人都使用的某个实用函数,那么就让它在某个名称空间中对所有人都可用,比如在这个名为
实用程序的名称空间中:
//Utility module (function(ns){ //declaring someFunction in the Utility namespace //it's available outside the ready handler, but lives in a namespace ns.someFunction = function(){...} }(this.Utility = this.Utility || {})); $(function(){ //here in the ready handler, we use it Utility.someFunction(); });
- 如果它们都生活在
处理程序中,并且希望处理程序中的所有代码都使用它,则在处理程序的最外层声明它,以便所有嵌套作用域都能看到它ready
$(function(){ //declare it in the outermost in the ready handler function someFunction(){...} //so we can use it even in the deepest nesting function nestedSomeFunction(){ someFunction(); } someElement.on('click',function(){ $.get('example.com',function(){ someFunction(); }); }); nestedSomeFunction(); someFunction(); });
$(文档)。ready
在$(文档)中。ready
?$(函数(){});
是$(文档)的简写版本。ready
您打算如何调用它?DOM事件?还是从另一个函数调用它?@Travis J感谢您提供的信息,现在它回答了我的问题“为什么”@约瑟夫,梦想家,我想从另一个角度来称呼它function@JobertEnamno这个函数位于文档的外部。ready
?为什么要使用$(文档)。ready
位于$(文档)的内部。ready
?$(函数(){});
是$(文档)的简写版本.ready
您打算如何调用它?DOM事件?还是从其他函数调用?@Travis J感谢您提供的信息现在它回答了我的问题“为什么”@约瑟夫,梦想家,我想从另一个角度来称呼它function@JobertEnamno此函数位于文档之外。准备好了吗?
?不要将它们设置为全局。将它们分配到不同的作用域,但不要将它们设置为全局。@IngoBürk-这取决于OP尝试执行的操作。如果他们提供了更多详细信息,我们可以提供其他选项,但r代码显然只是问题的一个例子,因此我们不知道它们真正在做什么或试图解决什么。全局函数在某些情况下很有用,正如我在回答中提到的其他构造一样。(不是downvoter)这是一种方法,但绝对不是理想的方法。您可以始终将其附加到另一个全局变量…例如使用jquery提供的$.fn
可用功能。至少这将利用现有结构。@jfriend00在某些情况下是否有用,在没有适当警告的情况下谈论全局函数mho总是不好的建议。也许你可以在你的答案中涵盖它?我很乐意改变我的投票结果。@IngoBürk-我在我的答案中添加了一些其他信息,但我不会把我的答案变成关于全局变量的论文。OP没有关于这里到底需要什么的细节。我已经回答了问题