博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
What does the dot after dollar sign mean in jQuery when declaring variables?
阅读量:4677 次
发布时间:2019-06-09

本文共 5478 字,大约阅读时间需要 18 分钟。

I see variables declared as:

$.root = $("body");

and

$root = $("body");

What is the difference between the two?

 

答案

Functions in JavaScript are objects. And like most objects in JavaScript, you can arbitrarily add properties to them. The $ function is just that, a function. So if you want to pop a property onto it and reference a jQuery collection, or reference, you can.

By adding the collection as a property on the $ function, it is one less variable in the current scope. You can examine the keys of the jQuery function before and after if you'd like to see how it affects the function's topography and (enumerable) property list:

Object.keys($); // ["fn", "extend", "expando"..."parseHTML", "offset", "noConflict"] $.root = $("body"); // [] Object.keys($); // ["fn", "extend", "expando"..."parseHTML", "offset", "noConflict", "root"]

 

这个不就是跟对象字面量一个意思,在jquery的对象上定义一个属性或方法,后面你可以直接用。let obj = {name:'aaa'},后面你就可以直接用obj.name。$.ajax就是这样的

 

 

 实例

$.widget('blueimp.fileupload', $.blueimp.fileupload, {})

这个widget的定义在

$.widget = function( name, base, prototype ) {    var fullName, existingConstructor, constructor, basePrototype,        // proxiedPrototype allows the provided prototype to remain unmodified        // so that it can be used as a mixin for multiple widgets (#8876)        proxiedPrototype = {},        namespace = name.split( "." )[ 0 ];    name = name.split( "." )[ 1 ];    fullName = namespace + "-" + name;    if ( !prototype ) {        prototype = base;        base = $.Widget;    }    // create selector for plugin    $.expr[ ":" ][ fullName.toLowerCase() ] = function( elem ) {        return !!$.data( elem, fullName );    };    $[ namespace ] = $[ namespace ] || {};    existingConstructor = $[ namespace ][ name ];    constructor = $[ namespace ][ name ] = function( options, element ) {        // allow instantiation without "new" keyword        if ( !this._createWidget ) {            return new constructor( options, element );        }        // allow instantiation without initializing for simple inheritance        // must use "new" keyword (the code above always passes args)        if ( arguments.length ) {            this._createWidget( options, element );        }    };    // extend with the existing constructor to carry over any static properties    $.extend( constructor, existingConstructor, {        version: prototype.version,        // copy the object used to create the prototype in case we need to        // redefine the widget later        _proto: $.extend( {}, prototype ),        // track widgets that inherit from this widget in case this widget is        // redefined after a widget inherits from it        _childConstructors: []    });    basePrototype = new base();    // we need to make the options hash a property directly on the new instance    // otherwise we'll modify the options hash on the prototype that we're    // inheriting from    basePrototype.options = $.widget.extend( {}, basePrototype.options );    $.each( prototype, function( prop, value ) {        if ( !$.isFunction( value ) ) {            proxiedPrototype[ prop ] = value;            return;        }        proxiedPrototype[ prop ] = (function() {            var _super = function() {                    return base.prototype[ prop ].apply( this, arguments );                },                _superApply = function( args ) {                    return base.prototype[ prop ].apply( this, args );                };            return function() {                var __super = this._super,                    __superApply = this._superApply,                    returnValue;                this._super = _super;                this._superApply = _superApply;                returnValue = value.apply( this, arguments );                this._super = __super;                this._superApply = __superApply;                return returnValue;            };        })();    });    constructor.prototype = $.widget.extend( basePrototype, {        // TODO: remove support for widgetEventPrefix        // always use the name + a colon as the prefix, e.g., draggable:start        // don't prefix for widgets that aren't DOM-based        widgetEventPrefix: existingConstructor ? (basePrototype.widgetEventPrefix || name) : name    }, proxiedPrototype, {        constructor: constructor,        namespace: namespace,        widgetName: name,        widgetFullName: fullName    });    // If this widget is being redefined then we need to find all widgets that    // are inheriting from it and redefine all of them so that they inherit from    // the new version of this widget. We're essentially trying to replace one    // level in the prototype chain.    if ( existingConstructor ) {        $.each( existingConstructor._childConstructors, function( i, child ) {            var childPrototype = child.prototype;            // redefine the child widget using the same prototype that was            // originally used, but inherit from the new version of the base            $.widget( childPrototype.namespace + "." + childPrototype.widgetName, constructor, child._proto );        });        // remove the list of existing child constructors from the old constructor        // so the old child constructors can be garbage collected        delete existingConstructor._childConstructors;    } else {        base._childConstructors.push( constructor );    }    $.widget.bridge( name, constructor );    return constructor;};

 

转载于:https://www.cnblogs.com/chucklu/p/11064856.html

你可能感兴趣的文章
Android模拟器无法上网访问网络失败解决办法
查看>>
node启动时, listen EADDRINUSE 报错;
查看>>
vue学习链接
查看>>
Systemd 初始化进程
查看>>
【C#学习笔记】文本复制到粘贴板
查看>>
Windows store 验证你的 URL http:// 和 https:// ms-appx:/// ms-appdata:///local
查看>>
python全栈开发_day7_字符编码,以及文件的基本读取
查看>>
js 验证码 倒计时60秒
查看>>
C#基础
查看>>
ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 15. 用户管理
查看>>
杭电3466————DP之01背包(对状态转移方程的更新理解)
查看>>
算法分析常用记号
查看>>
3.1.3自适应阈值化
查看>>
NABCD
查看>>
ZOJ 2850 Beautiful Meadow (简单题)
查看>>
Android开源框架ImageLoader的完美例子
查看>>
LeetCode - Best Time to Buy and Sell Stock
查看>>
java-Coculator
查看>>
499 单词计数 (Map Reduce版本)
查看>>
python笔记
查看>>