javascript和jquery中的方法和函数重载

时间:2022-08-08 16:59:38

Can I use method and function overload in java-script and JQuery, if possible it means how to do that.

我可以在java脚本和JQuery中使用方法和函数重载,如果可能的话,它意味着如何做到这一点。

I have developed following code, but I think that is wrong, what's my error I don't know that, please clarify that

我开发了以下代码,但我认为这是错误的,我不知道的是什么错误,请澄清一下

    function isnull(txtid, errid, err) {  
        if (trimString(get(txtid).value) == '') {
            return false;
        }
        return true;
    }

    function isnull(txtid) {
        if (trimString(get(txtid).value) == '') {        
            return false;
        }
        return true;
    }

All the time function calls second one only, if any idea do this one

所有的时间函数只调用第二个,如果有任何想法这样做

2 个解决方案

#1


6  

There is no real function overloading in JavaScript because javascript has no type checking on arguments or required qty of arguments, you can just have one implementation of isnull that can adapt to what arguments were passed to it by checking the type, presence or quantity of arguments.

JavaScript中没有真正的函数重载,因为javascript没有对参数进行类型检查或需要参数的数量,你可以只有一个isnull实现,它可以通过检查参数的类型,存在或数量来适应传递给它的参数。

The best way to do function overloading with parameters is not to check the argument length or the types; checking the types will just make your code slow and you have the fun of Arrays, nulls, Objects, etc.

使用参数进行函数重载的最佳方法是不检查参数长度或类型;检查类型只会让你的代码变慢,你可以享受Arrays,nulls,Objects等的乐趣。

What most developers do is tack on an object as the last argument to their methods. This object can hold anything.

大多数开发人员所做的是将对象作为其方法的最后一个参数。这个对象可以容纳任何东西

function foo(a, b, opts) {

}


foo(1, 2, {"method":"add"});
foo(3, 4, {"test":"equals", "bar":"tree"});

Then you can handle it anyway you want in your method. [Switch, if-else, etc.]

然后你可以在你的方法中处理它。 [切换,if-else等]

#2


2  

A trick to do overloading would be like this:

重载的技巧是这样的:

function over_load(object, name, args) {
  var prev = object[name];
  object[name] = function(){
  if (args.length == arguments.length)
    return fn.apply(this, arguments);
  else if (typeof prev == 'function')
    return prev.apply(this, arguments);
  else throw "Wrong number of args"; 
  };
}

but according to the answer Function overloading in Javascript - Best practices , checking the argument length makes it slow, so there is a better way to achieve overloading i.e by passing an object.. (check link for reference)

但根据答案Javascript函数重载 - 最佳实践,检查参数长度使它变慢,所以有一种更好的方法来实现重载,即通过传递一个对象..(检查链接以供参考)

#1


6  

There is no real function overloading in JavaScript because javascript has no type checking on arguments or required qty of arguments, you can just have one implementation of isnull that can adapt to what arguments were passed to it by checking the type, presence or quantity of arguments.

JavaScript中没有真正的函数重载,因为javascript没有对参数进行类型检查或需要参数的数量,你可以只有一个isnull实现,它可以通过检查参数的类型,存在或数量来适应传递给它的参数。

The best way to do function overloading with parameters is not to check the argument length or the types; checking the types will just make your code slow and you have the fun of Arrays, nulls, Objects, etc.

使用参数进行函数重载的最佳方法是不检查参数长度或类型;检查类型只会让你的代码变慢,你可以享受Arrays,nulls,Objects等的乐趣。

What most developers do is tack on an object as the last argument to their methods. This object can hold anything.

大多数开发人员所做的是将对象作为其方法的最后一个参数。这个对象可以容纳任何东西

function foo(a, b, opts) {

}


foo(1, 2, {"method":"add"});
foo(3, 4, {"test":"equals", "bar":"tree"});

Then you can handle it anyway you want in your method. [Switch, if-else, etc.]

然后你可以在你的方法中处理它。 [切换,if-else等]

#2


2  

A trick to do overloading would be like this:

重载的技巧是这样的:

function over_load(object, name, args) {
  var prev = object[name];
  object[name] = function(){
  if (args.length == arguments.length)
    return fn.apply(this, arguments);
  else if (typeof prev == 'function')
    return prev.apply(this, arguments);
  else throw "Wrong number of args"; 
  };
}

but according to the answer Function overloading in Javascript - Best practices , checking the argument length makes it slow, so there is a better way to achieve overloading i.e by passing an object.. (check link for reference)

但根据答案Javascript函数重载 - 最佳实践,检查参数长度使它变慢,所以有一种更好的方法来实现重载,即通过传递一个对象..(检查链接以供参考)