`
kkkhhhli
  • 浏览: 11627 次
  • 性别: Icon_minigender_1
  • 来自: 南京
最近访客 更多访客>>
社区版块
存档分类
最新评论

JQuery 命名空间

阅读更多

jQuery encourages using namespaces for methods in the $ namespace, like $.foo.bar() rather than $.bar() . This works for $ because methods don't expect this to refer to anything specific, and the way javascript works is to assign this to the last-named object, so in $.foo.bar() , this refers to $.foo .

This idea fails for plugins, however, since plugins expect this to refer to the jQuery object that started the chain. If I define $.fn.bar = function (){ } , then when $(...).bar() is called, this refers to $(...) , just as we want. But if I define $.fn.foo.bar = function (){ } , then when $(...).foo.bar() is called, this refers to $(...).foo , which is an object that knows nothing about jQuery. There's no way to make an object reference return something else.

But all is not lost. We can define a function that returns an object, and that function can use this to set the returned object to be just like a jQuery object, but with the desired namespaced methods in it. The inefficient way to do that is to copy the new methods into the jQuery object, but if we can manipulate the prototype chain directly (as we can in Firefox) we can add our new methods to the chain without copying.

So a namespacing plugin would be:

  1. ( function  ($) {  
  2.     if ({}.__proto__) {  
  3.         // mozilla & webkit expose the prototype chain directly   
  4.         $.namespace=function  (name) {  
  5.             $.fn[name]=function  namespace() {  
  6.                 // insert this function in the prototype chain   
  7.                 this .__proto__=arguments.callee;  
  8.                 return   this ;  
  9.             };  
  10.             $.fn[name].__proto__=$.fn;  
  11.         };  
  12.         $.fn.$=function  () {  
  13.             this .__proto__=$.fn;  
  14.             return   this ;  
  15.         };  
  16.     }  
  17.     else  {  
  18.         // every other browser; need to copy methods   
  19.         $.namespace=function  (name) {  
  20.             $.fn[name]=function  namespace() {  
  21.                 return   this .extend(arguments.callee);  
  22.             };  
  23.         };  
  24.         $.fn.$=function  () {  
  25.             // slow but restores the default namespace   
  26.             var  len= this .length;  
  27.             this .extend($.fn);  
  28.             this .length=len;  
  29.             // $.fn has length = 0, which messes everything up   
  30.             return   this ;  
  31.         };  
  32.     }  
  33. })(jQuery);  

 

And you could use it like:

  1. $.namespace( 'danny' );  
  2. $.namespace('danny2' );  
  3. $.fn.danny.foo=function  () {  
  4.     return   this .css( 'color' , 'green' )  
  5. };  
  6. $.fn.danny2.foo=function  (x) {  
  7.     alert(x);  
  8.     return   this ;  
  9. };  
  10. // now we have two different methods "foo"   
  11. $('p' ).danny().foo();  
  12. // colors paragraphs green   
  13. $('p' ).danny2().foo( 'Hello, world' );  
  14. // alerts 'Hello, world'   
  15. $('p' ).danny().foo().danny2().foo( 'Hello, world' );  
  16. // chaining works   
  17. $.fn.danny.add=function  (a,b) {  
  18.     alert(a+b);  
  19.     return   this ;  
  20. };  
  21. // defines a function with the same name as a real jQuery one   
  22. $('p' ).danny().add(1,2).$().add( 'div' );  
  23. // the $() plugin restores the real jQuery namespace to a chain    

The namespacing is per-chain only; $('p' ).danny() does not affect any subsequent statements. Plugins that call pushStack will reset the namespacing, but in general the namespace function should be called right before the method, so that should not be an issue.

This is inefficient, obviously, adding an extra function call and possible a lot of copying with extend , but for most code that is insignificant.

分享到:
评论

相关推荐

    jquery命名空间模拟

    GLOBAL 文件 博文链接:https://xujunxiong.iteye.com/blog/1860329

    jQuery命名空间与闭包用法示例

    本文实例讲述了jQuery命名空间与闭包用法。分享给大家供大家参考,具体如下: /* * 服务公司用户汇总,审核 */ (function() { "use strict"; var companyList=new Object();//声明命名空间 //时间戳格式化为...

    jquery利用命名空间移除绑定事件的方法

    主要介绍了jquery利用命名空间移除绑定事件的方法,实例分析了jQuery命名空间及事件绑定的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

    jquery自定义插件命名空间问题

    NULL 博文链接:https://i5land.iteye.com/blog/350151

    jquery解析带名称空间的xml

    jquery.xmlns-1.7.0.js 是根据jquery.xmlns.js改写的适用于各种版本的jquery解析带命名空间的XML数据,里面附有实例代码,如果使用中有遇到问题,可以反馈,我会进一步改进。

    jQuery完全实例.rar

    jQuery1.2 API 中文版折叠展开折叠全部展开全部 英文说明 核心jQuery 核心函数 jQuery(expression,[context]) jQuery(expression,[context]) 这个函数接收一个包含 CSS 选择器的字符串,然后用这个字符串去匹配一组...

    jQuery插件库

    jQuery插件开发全解析 jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,...jQuery 的全局函数就是属于jQuery命名空间的函数,另一种是对象级别的插件开发,即给jQuery对象添加方法。

    一步一步教你写一个jQuery的插件教程(Plugin)

    明确jQuery的命名空间只有一个。2. 明白options参数用来控制plugin的行为。3. 为默认的plugin设定提供公共的访问权限。4. 为子函数提供公共的访问权限。5. 私有的函数绝对是私有访问6. 支持metadata plugin。...

    jQuery插件开发全解析

    jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级别的插件开发,即给jQuery对象添加方法。下面就两种函数的开发做详细的说明。 1、类级别的插件开发 类级别的插件开发最直接的理解就是给jQuery类添加...

    jQuery中绑定事件的命名空间详解

    在没有看到这篇 文章之前,我一直不知道原来bind也可以有命名空间。事实上,我看完这篇文章后,再去翻了一下手册,也才发现了一点点的注释。但手册也仅仅是一句话就带 过去了。没有过多的深究,或许他认为命名空间这...

    如何解决jQuery 和其他JS库的冲突

    在 jQuery 库中,几乎所有的插件都被限制在它的命名空间里。通常,全局对象都被很好地存储在 jQuery 命名空间里,因此当把 jQuery 和其他 JavaScript 库(例如 Prototype、MooTools 或 YUI)一起使用时,不会引起...

    jQuery编程中的一些核心方法简介

    但是也有一些方法不需要依赖于选择器的结果集,这些方法位于 jQuery 命名空间内,这些方法称为 jQuery 核心方法。如果觉得不好理解,记住下面两条即可: 所有 jQuery 选择器的方法都位于 $.fn 命名空间内。 $ 内的...

    jQuery插件原来如此简单 jQuery插件的机制及实战

    2、封装全局函数 可以将独立的函数加到jQuery命名空间下。如常用的jQuery.ajax()方法、去首尾空格的jQuery.trim()方法,都是jQuery内部作为全局函数的插件附加到内核上去的。 3、选择器插件 虽然jQuery的选择器十分...

    Jquery插件写法笔记整理

    全局函数: 也可以把自定义的功能函数独立附加到jQuery命名空间下,从而作为jQuery作用域下的一个公共函数使用。但全局函数没有被绑定到jQuery对象上,故不能在选择器获取的jQuery对象上调用。需要通过jQuery.fn()...

    jQuery插件制作的实例教程

    可以把自定义的功能函数独立附加到jQuery命名空间下,从而作为jQuery作用域下的一个公共函数使用。 但全局函数没有被绑定到jQuery对象上,故不能在选择器获取的jQuery对象上调用。需要通过jQuery.fn()或$.fn()方式...

    详解jQuery插件开发方式

    一般来说,jQuery插件的开发分为两种:一种是挂在jQuery命名空间下的全局函数,也可称为静态方法;另一种是jQuery对象级别的方法,即挂在jQuery原型下的方法,这样通过选择器获取的jQuery对象实例也能共享该方法。 ...

    jQuery.switchPage.js:基于jQuery的全屏切换插件

    一、类级别的组件开发:即给jQuery命名空间下添加新的全局函数,称为静态方法。 jQuery.foo = function() { // do something } // 使用 $.foo() 进行调用 例如 $.Ajax() 、 $.extend() 方法 二、对象级别组件开发:...

    jQuery插件制作之全局函数用法实例

    所谓的全局函数,实际上就是jQuery对象的方法,但从实践的角度上看,他们是位于jQuery命名空间内部的函数 (1)添加一个函数,只需要将新函数指定为jQuery对象的一个属性。 jQuery.five =function(){ alert(...

Global site tag (gtag.js) - Google Analytics