jQuery fn.extend ({bla: function(){}} 与 jQuery.fn.bla [英] jQuery fn.extend ({bla: function(){}} vs. jQuery.fn.bla

查看:12
本文介绍了jQuery fn.extend ({bla: function(){}} 与 jQuery.fn.bla的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我想我明白了jQuery.extend 和 jQuery.fn.extend 的区别? p>

因为通用扩展可以扩展任何对象,并且 fn.extend 用于插件函数,可以使用一些内部 jquery voodoo 直接从 jquery 对象调用.

所以看起来人们会以不同的方式调用它们.如果您使用通用扩展通过添加函数 y 来扩展对象 obj,那么该方法将附加到该对象 obj.y() 但如果您使用 fn.extend 那么它们是直接附加到 jquery 对象 $.y().... 我是否得到正确的是或否,如果没有,我的理解有什么问题?

现在我的问题:

我正在阅读的书提倡使用

jQuery.fn.extend({一个:函数(){},b:函数(){}});

语法,但在文档中它说

jQuery.fn.a = function() { };

我想如果你也想要 b 的话

jQuery.fn.b = function() { };

这些在功能和性能方面是否等效?如果不是,有什么区别?

非常感谢.我正在挖掘 jQuery!

解决方案

它们之间有非常细微的区别.性能方面(不是问题), jQuery.fn.extend 会比直接声明像 jQuery.fn.foo = function(){ };.我说的是完全可以忽略不计的差异.

不同之处在于 jQuery.fn.extend() 允许您同时添加多个插件函数,并且可能会使您的代码看起来更简洁,具体取决于您正在创作的插件类型.p><小时>

jQuery.fn.a = function(){};jQuery.fn.b = 函数(){};

完全一样

jQuery.fn.extend({一个:函数(){},b:函数(){}});

OK I think I get Difference between jQuery.extend and jQuery.fn.extend?

in that the general extend can extend any object, and that fn.extend is for plugin functions that can be invoked straight off the jquery object with some internal jquery voodoo.

So it appears one would invoke them differently. If you use general extend to extend object obj by adding function y, then the method would attach to that object, obj.y() but if you use fn.extend then they are attach straight to the jquery object $.y().... Have I got that correct yes or no and if no what do I have wrong in my understanding?

Now MY question:

The book I am reading advocates using

jQuery.fn.extend({
    a: function() { },
    b: function() { }
});

syntax but in the docs it says

jQuery.fn.a = function() { };

and I guess if you wanted b as well it would be

jQuery.fn.b = function() { };

Are these functionally and performance-wise equivalent and if not what is the difference?

Thank you very much. I am digging jQuery!

解决方案

There's very subtle difference between them. Performance wise (not that this is an issue), jQuery.fn.extend is going to be slower than directly declaring a function like jQuery.fn.foo = function(){ };. I'm talking completely negligible difference.

The difference is jQuery.fn.extend() allows you to add multiple plugin functions simultaneously and might make your code look a little cleaner depending on what type of plugin you're authoring.


jQuery.fn.a = function(){};
jQuery.fn.b = function(){};

Is exactly the same as

jQuery.fn.extend({
  a: function(){ },
  b: function(){ }
});

这篇关于jQuery fn.extend ({bla: function(){}} 与 jQuery.fn.bla的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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