我可以在JavaScript中复制/克隆一个函数吗? [英] Can I copy/clone a function in JavaScript?

查看:99
本文介绍了我可以在JavaScript中复制/克隆一个函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有验证器插件的jQuery。我想用自己的一个替换必需的验证器。这很简单:

I'm using jQuery with the validators plugin. I would like to replace the "required" validator with one of my own. This is easy:

jQuery.validator.addMethod("required", function(value, element, param) {
    return myRequired(value, element, param);
}, jQuery.validator.messages.required);

到目前为止,非常好。这很好用。但我真正想要做的是在某些情况下调用我的函数,以及其余的默认验证器。不幸的是,结果是递归的:

So far, so good. This works just fine. But what I really want to do is call my function in some cases, and the default validator for the rest. Unfortunately, this turns out to be recursive:

jQuery.validator.addMethod("required", function(value, element, param) {
    // handle comboboxes with empty guids
    if (someTest(element)) {
        return myRequired(value, element, param);
    }
    return jQuery.validator.methods.required(value, element, param);
}, jQuery.validator.messages.required);

我查看了验证器的源代码,并将required的默认实现定义为jQuery.validator.messages.required中的匿名方法。所以没有其他(非匿名)引用我可以使用的函数。

I looked at the source code for the validators, and the default implementation of "required" is defined as an anonymous method at jQuery.validator.messages.required. So there is no other (non-anonymous) reference to the function that I can use.

在调用addMethod并通过调用默认验证器之前,在外部存储对函数的引用该引用没有区别。

Storing a reference to the function externally before calling addMethod and calling the default validator via that reference makes no difference.

我真正需要做的是能够按值而不是通过引用复制默认的必需验证器函数。但经过相当多的搜索,我无法弄清楚如何做到这一点。有可能吗?

What I really need to do is to be able to copy the default required validator function by value instead of by reference. But after quite a bit of searching, I can't figure out how to do that. Is it possible?

如果不可能,那么我可以复制原始功能的来源。但这会造成维护问题,除非没有更好的方法,否则我宁愿不这样做。

If it's impossible, then I can copy the source for the original function. But that creates a maintenance problem, and I would rather not do that unless there is no "better way."

推荐答案


在调用addMethod
之前在外部存储对函数
的引用,并通过
调用默认验证器,该引用没有区别。

Storing a reference to the function externally before calling addMethod and calling the default validator via that reference makes no difference.

这正是应该起作用的

jQuery.validator.methods.oldRequired = jQuery.validator.methods.required;

jQuery.validator.addMethod("required", function(value, element, param) {
    // handle comboboxes with empty guids
    if (someTest(element)) {
        return myRequired(value, element, param);
    }
    return jQuery.validator.methods.oldRequired(value, element, param);
}, jQuery.validator.messages.required);

这也应该有效:(以及的问题已解决)

This should work too: (And the problem with this is solved)

var oldRequired = jQuery.validator.methods.required;
jQuery.validator.addMethod("required", function(value, element, param) {
    // handle comboboxes with empty guids
    if (someTest(element)) {
        return myRequired(value, element, param);
    }
    return oldRequired.call(this, value, element, param);
    // return jQuery.oldRequired.apply(this, arguments);
}, jQuery.validator.messages.required);

这篇关于我可以在JavaScript中复制/克隆一个函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆