What does Jquery.Recup mean?

What does Jquery.Recup mean?

本文关键字:mean Recup Jquery does What      更新时间:2023-09-26

我对"$"这行感到困惑。Recup……"我不知道为什么它被命名为相同的插件名称和它是什么。

(function ($) {
    $.fn.Recup = function () {
        var parametros = {
        };
        var tsic = true;
        $.Recup = function (opciones) {
            var Metodos = {
            };
            return Metodos;
        };
        $.Recup.anterior = function () {
        };
        $.Recup.siguiente = function () {
    }
   })(jQuery);

我指的是这段代码,$.Recup到底做了什么?如果有人能给我举个例子,那就太好了

         $.Recup = function (opciones) {
                var Metodos = {
                };
                return Metodos;
            };

在这种情况下,它似乎是一个有问题的插件设计- 特别是因为$.Recup直到$.fn.Recup首先被调用才被分配。

然而,如果它是"适当和/或写得很好"是另一个需要上下文(预期)用法的问题。不管它的价值是什么,我将拒绝这段代码,因为它散发出被误解的设计和广泛的副作用。


无论如何,函数的赋值方式决定了如何调用该方法。

// let $ be jQuery, then:
$.fn.foo = function () { console.log("foo") }
$.bar    = function () { console.log("bar") }
$.foo()        // TypeError: $.foo is not a function
$.bar()        // -> "bar"
$("sel").foo() // -> "foo"
$("sel").bar() // TypeError: $(..).bar is not a function

也就是说,$.fn.foo类似于.each()——它基于当前选择的元素(由this表示)做一些事情。另一方面,$.bar类似于jQuery.each()——它提供了一种遍历一般集合的方法,但与(先前)选定的特定元素集无关。

一般来说,一个插件应该只向$.fn添加一个条目,但是直接添加到$ 可能对于暴露实用函数是有用的——这绝对应该小心。


下面是修复错误泄露数据问题的两种方法:

$.fn.Recup = function () {
    var parametros = ..
    var tsic = true;
    // Most trivial change; then use recup in this scope
    // (or child scopes) only. There is no $.Recup - yay!
    var recup = function (opciones) {
    };
    // ..
}

或者,直接暴露为局部方法:

$.fn.Recup = function () {
    var parametros = ..
    var tsic = true;
    function anterior () {
    }
    function siguiente () {
    }
    // Just use simple functions in scope
}

这是一个jQuery插件。

jQuery.fn是jQuery原型的别名。这一行让您可以在jQuery实例上调用Recup函数:

$('#myid').Recup();

这是关于创建jQuery插件的文档