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

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

问题描述

好吧,我想我明白了 jQuery.extend与jQuery.fn.extend之间的区别?

,因为常规扩展可以扩展任何对象,并且fn.extend用于可以使用某些内部jquery voodoo直接在jquery对象上调用的插件函数.

因此,似乎会以不同的方式调用它们.如果使用通用扩展通过添加函数y扩展对象obj,则该方法将附加到该对象obj.y(),但是如果使用fn.extend则它们是 直接附加到jquery对象$ .y()....我是否正确,是或否,如果没有,我理解上有什么错误吗?

现在我的问题:

我正在阅读的书提倡者使用

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

语法,但在文档中显示

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

我想你是否也想要b

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

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

非常感谢.我在挖jQuery!

解决方案

它们之间有非常细微的区别.从性能角度考虑(不是这是个问题),jQuery.fn.extend比直接声明类似jQuery.fn.foo = function(){ };的函数要慢.我说的是完全可以忽略的差异.

区别在于jQuery.fn.extend()允许您同时添加多个插件功能,并可能使代码看起来更简洁,具体取决于您编写的插件类型.


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

完全相同

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

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天全站免登陆