我应该传递 jQuery 或 DOM 对象作为参数吗? (性能问题)

发布于 2024-12-06 16:43:28 字数 253 浏览 2 评论 0原文

哪个性能更好。

foo(this);

function foo(element) {

    $(element).index();

}

或者我应该

foo($(this));

function foo($element) {

    $element.index();

}

显然考虑到我将在函数内多次使用该参数。

谢谢! 康纳

Which is better performance wise.

foo(this);

function foo(element) {

    $(element).index();

}

Or should I do

foo($(this));

function foo($element) {

    $element.index();

}

Obviously taking into account that I will be using the argument a fair few times inside the function.

Thanks!
Connor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深居我梦 2024-12-13 16:43:28

如果您无论如何都要包装对象,则将对象包装在 jQuery 上的位置并不重要。

唯一重要的是您缓存包装结果并且不要包装两次。

就此而言,以下规则适用于许多插件的代码:

1) jQuery 变量均以 $ 为前缀: var $this = $(this)

2) 切勿将 $ 前缀的 var 包装在 $ 中

3)始终缓存(保存到 var)任何多次使用的 jQuery 包装表达式

4) 如果相同的包装对象(例如 var $items = $('ul li');)出现多次几个类似的函数,将其移至外部作用域并依赖闭包。

It doesn't matter where you wrap an object on jQuery if you're going to wrap it anyway.

It only matters that you cache the wrapping result and don't wrap it twice.

For that matter the following rules apply to many plugins' code:

1) jQuery vars are all prefixed with $: var $this = $(this)

2) never wrap $-prefixed var in $

3) always cache (save to var) any jQuery-wrapped expression used more than once

4) if the same wrapped object (like var $items = $('ul li');) occurs more than once in several similar functions, move it to the outer scope and rely on closure.

太阳男子 2024-12-13 16:43:28

如果您正在编写一个将一个 jQuery 对象作为参数的函数,那么您确实应该考虑将其编写为 jQuery 插件。

jQuery.fn.yourFunction = function(otherArg1, otherArg2, ...) {
  // ...
};

而不是编写

yourFunction($(whatever));

然后,您可以

$(whatever).yourFunction().someOtherJQueryFunction();

在函数内部编写, ,this 值将是 jQuery 对象本身。大多数常见的 DOM 相关函数使用的模式是:

jQuery.fn.yourFunction = function(otherArg1, otherArg2, ...) {
  return this.each(function() {
    var $element = $(this);
    // do stuff ...
  });
};

请注意,在函数的外层, this 没有包装为 $(this) 因为它已经被保证了成为一个 jQuery 对象。在“each()”函数或其他类似函数的主体中,情况并非如此。

If you're writing a function that's going to take one jQuery object as a parameter, you really should consider writing it as a jQuery plugin instead.

jQuery.fn.yourFunction = function(otherArg1, otherArg2, ...) {
  // ...
};

Then instead of writing

yourFunction($(whatever));

you can write

$(whatever).yourFunction().someOtherJQueryFunction();

Inside the function, the this value will be the jQuery object itself. A pattern to use for most common DOM-related functions is:

jQuery.fn.yourFunction = function(otherArg1, otherArg2, ...) {
  return this.each(function() {
    var $element = $(this);
    // do stuff ...
  });
};

Note that in the outer level of the function, this is not wrapped as $(this) because it's already guaranteed to be a jQuery object. That is not the case in the body of an "each()" function, or anything else like that.

预谋 2024-12-13 16:43:28

如果您只想将单个元素传递给函数,那么这并不重要。我会根据调用时可能已经掌握的参数来设计函数参数。如果我在调用时从未在 jQuery 对象中拥有它,那么我只需传递一个 DOM 元素并让函数将其放入 jQuery 对象(如果需要)。如果我总是已经在 jQuery 对象中拥有它,那么传递我已经拥有的该对象而不是额外的 DOM 元素,然后将其放入函数内的另一个 jQuery 对象中,效果会更好。

如果您打算将多个元素传递给函数,那么传递 jQuery 对象可能会更容易,因为它是多个对象的一个​​很好方便的包装器。

正如 Pointy 所说,最优雅的解决方案是让你的函数成为一个插件,然后它会自动接受许多不同的参数,包括选择器、DOM 元素、DOM 元素数组或 jQuery 对象(所有这些都由 jQuery 基础设施为你处理) )。

如果您只将 DOM 元素变成 jQuery 对象一次,您将获得最佳性能。传递 DOM 或传递 jQuery 对象作为参数将具有相同的性能(两者都传递对对象的引用)。插件的想法实际上会具有稍差的性能,因为它是如此通用并且接受许多不同的参数,因此它必须识别传递的参数类型。与 .index() 方法本身所花费的时间相比,这种性能差异可能并不明显。

If you intend to only pass a single element to the function, then it doesn't really matter. I would design the function parameter depending upon what I was likely to already have handy at the time of calling. If I never had it in a jQuery object already at the time of calling, then I would just pass a DOM element and let the function make it into a jQuery object (if needed). If I always already had it in a jQuery object, then it will perform better to pass that object that I already have rather than extra the DOM element, then make it into another jQuery object inside the function.

If you intend to pass multiple elements to the function, then it's probably easier to just pass a jQuery object because it's a nice convenient wrapper for the multiple objects.

As Pointy said, the most elegant solution is to make your function a plugin and then it will automatically accept lots of different arguments including a selector, a DOM element, a DOM element array or a jQuery object (all handled for you by the jQuery infrastructure).

You will achieve the best performance if you only make the DOM element into a jQuery object once. Passing a DOM or passing a jQuery object as the argument will have the same performance (both are passing a reference to an object). The plug-in idea will actually have slightly worse performance because it is so general purpose and accepts lots of different arguments and thus it has to identify what type of argument was passed. That performance difference is perhaps not noticeable compared to the time that the .index() method takes itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文