好吧,我对女巫所说的“$.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;
};
最佳答案
在这种情况下,它似乎是一个有问题的插件设计 - 特别是因为在首次调用 $.fn.Recup
之前不会分配 $.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
}
关于javascript - Jquery.Recup 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16875996/