jQuery函数调用
我有两个jQuery函数。理论上一个叫另一个。。。。这些是:jQuery函数调用,jquery,function,Jquery,Function,我有两个jQuery函数。理论上一个叫另一个。。。。这些是: $.testFunction = function(arg1){ alert("testFunction(arg1)"); $.testFunction(arg1, ""); } $.testFunction = function(arg1, arg2){ alert("testFunction(arg1, arg2)"); alert("arg1: " + arg1 + "\narg2: " + a
$.testFunction = function(arg1){
alert("testFunction(arg1)");
$.testFunction(arg1, "");
}
$.testFunction = function(arg1, arg2){
alert("testFunction(arg1, arg2)");
alert("arg1: " + arg1 + "\narg2: " + arg2);
}
我有两个函数,因为当我没有第二个要传递的参数时,我想调用它们的简单版本。
但当我这样打电话时:
$.testFunction("first param");
alert("Before second call");
$.testFunction("first param", "second param");
$.testFunction = function(arg1, arg2){
if(arguments.length == 1){
// handle one argument
}else if(arguments.length == 2{
// handle 2 arguments
}
}
它总是调用第二个参数,并在警报窗口中放入:testFunctionarg1、arg2,然后是arg1:第一个参数arg2:未定义。为什么要这样工作?当我只传递一个参数时,为什么不调用第一个函数 javascript中没有函数重载,您的第二个函数将替换第一个函数 通过检查arguments对象可以实现类似的功能,如下所示:
$.testFunction("first param");
alert("Before second call");
$.testFunction("first param", "second param");
$.testFunction = function(arg1, arg2){
if(arguments.length == 1){
// handle one argument
}else if(arguments.length == 2{
// handle 2 arguments
}
}
javascript中没有函数重载,您的第二个函数将替换第一个函数 通过检查arguments对象可以实现类似的功能,如下所示:
$.testFunction("first param");
alert("Before second call");
$.testFunction("first param", "second param");
$.testFunction = function(arg1, arg2){
if(arguments.length == 1){
// handle one argument
}else if(arguments.length == 2{
// handle 2 arguments
}
}
嗯-您正在立即覆盖第一个函数。以下是与您所做的工作相同的内容:
x = "foo";
x = "bar";
alert(x); // 'bar' -- "why isn't this foo????!?!"
一个好的选择是编写一个函数,该函数根据传递给它的参数数量的不同而表现出不同的行为:
var testFunction = function(a, b) {
if (b === undefined) {
// no second parameter
}
};
嗯-您正在立即覆盖第一个函数。以下是与您所做的工作相同的内容:
x = "foo";
x = "bar";
alert(x); // 'bar' -- "why isn't this foo????!?!"
一个好的选择是编写一个函数,该函数根据传递给它的参数数量的不同而表现出不同的行为:
var testFunction = function(a, b) {
if (b === undefined) {
// no second parameter
}
};
Javascript不支持方法重载——至少在传统意义上是这样
第二个函数覆盖了第一个函数。Javascript不支持方法重载,至少在传统意义上是这样
第二个函数正在覆盖第一个函数。您正在覆盖该函数。Javascript没有重载函数的概念 相反,函数接受任意数量的参数,您可以通过special arguments属性访问它们
$.testFunction = function(arg1, arg2){
if(arguments.length == 2){
alert("arg1: " + arg1 + "\narg2: " + arg2);
}else{
alert("arg1: " + arg1);
}
}
您正在覆盖该函数。Javascript没有重载函数的概念 相反,函数接受任意数量的参数,您可以通过special arguments属性访问它们
$.testFunction = function(arg1, arg2){
if(arguments.length == 2){
alert("arg1: " + arg1 + "\narg2: " + arg2);
}else{
alert("arg1: " + arg1);
}
}
您正在重新定义函数,并将第一个单参数函数有效地替换为一个双参数函数。现在你真的只有一个功能了
您可能想知道哪个函数可能有助于重载。您正在重新定义函数,并将第一个单参数函数有效地替换为一个双参数函数。现在你真的只有一个功能了
$.testFunction = function(arg1, arg2){
if(arg2 === null || arg2 === undefined){
// run the first version
}else{
// run the second version
}
}
您可能想知道这可能有助于过载
$.testFunction = function(arg1, arg2){
if(arg2 === null || arg2 === undefined){
// run the first version
}else{
// run the second version
}
}
请尝试这样做-这样,您只有一个函数,并且在执行主体之前只需检查第二个参数是否存在
试着这样做-这样,您只有一个函数,您只需在执行主体之前检查第二个参数是否存在。谢谢大家!我想,这就像在java中一样,我可以编写重载方法谢谢大家!我想,这就像在java中一样,我可以编写重载方法