• What does the dot after dollar sign mean in jQuery when declaring variables?


    https://stackoverflow.com/questions/22156664/what-does-the-dot-after-dollar-sign-mean-in-jquery-when-declaring-variables

    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");
    // [<body>]
    
    Object.keys($);
    // ["fn", "extend", "expando"..."parseHTML", "offset", "noConflict", "root"]
    这个不就是跟对象字面量一个意思,在jquery的对象上定义一个属性或方法,后面你可以直接用。let obj = {name:'aaa'},后面你就可以直接用obj.name。$.ajax就是这样的

     实例

    https://github.com/tkvw/jQuery-File-Upload/blob/master/js/jquery.fileupload-image-editor.js

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

    这个widget的定义在https://github.com/tkvw/jQuery-File-Upload/blob/master/js/vendor/jquery.ui.widget.js#L57

    $.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;
    };
  • 相关阅读:
    JavaScript 语句
    JavaScript语法
    JavaScript输出
    JavaScript用法
    JavaScript简介
    利用IDEA构建springboot应用-构建好SpringBoot + SSM 框架
    SP2010开发和VS2010专家"食谱"--第二章节--工作流(4)--创建自定义任务表单
    SP2010开发和VS2010专家"食谱"--第二章节--工作流(3)--创建工作流里的任务
    SP2010开发和VS2010专家"食谱"--第二章节--工作流(2)--部署工作流的InfoPath表单
    SP2010开发和VS2010专家"食谱"--第二章节--工作流(1)--创建带有初始表单的网站工作流
  • 原文地址:https://www.cnblogs.com/chucklu/p/11064856.html
Copyright © 2020-2023  润新知