JQuery 命名空间
来源:互联网 发布:八爪鱼 软件 编辑:程序博客网 时间:2024/05/29 03:18
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:
And you could use it like:
The namespacing is per-chain only; This is inefficient, obviously, adding an extra function call and possible a lot of copying with $('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.extend
, but for most code that is insignificant.
- JQuery 命名空间
- jQuery 命名空间函数
- jQuery bind命名空间
- jQuery bind命名空间
- jquery命名空间
- jQuery事件命名空间
- jquery事件命名空间
- JQuery自定义命名空间
- 命名用jquery扩充命名空间
- jQuery 事件的命名空间
- jQuery 事件的命名空间
- jquery命名空间与模块化
- jquery命名空间,插件开发
- JQuery 中的事件命名空间
- jQuery 命名空间、闭包
- jQuery中事件命名空间
- jQuery中事件命名空间
- 【jQuery学习笔记-----jQuery事件命名空间】
- MeeGo v1.1 SDK
- 关于C/C++的一些链接
- 站稳马步——(9)java异常处理
- CODE RO RW ZI
- find_first_of()和 find_last_of()
- JQuery 命名空间
- 逼自己一把,你就优秀了 【转】
- 如何显示vim行号和自动缩进 设置ts=4
- mysql 安装抱内存错误 0x005ea630
- 属性项目的定义(property)
- from-an-architect-to-a-programmer
- 恐怖的MySql-bin.0000X日志文件(转http://www.jiucool.com/terror-mysql-bin-0000x-log-file/)
- PHP5中Date()函数获取时间相差8小时问题的解决方法
- 类型转换