zoukankan      html  css  js  c++  java
  • jQuery中对象的构建

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>jQuery</title>
        <script src="jquery.js"></script>
    </head>
    <body>
    <div id="aaron">jQuery</div>
    
    <script type="text/javascript">
    
        (function(window, factory){
            factory(window);
        }(typeof window !== undefined ? window : this, function(window, noGlobal){
            var $jQuery = function(selector, context){
                return new $jQuery.fn.init( selector, context );
            }
            $jQuery.fn = $jQuery.prototype = {
                init: function(){
                    this.name = "aa";
                    return this;
                },
                constructor: $jQuery
            }
            if(typeof noGlobal === "undefined"){
                window.$jQuery = $jQuery;
            }
            return $jQuery;
        }))
        var a = $jQuery();
        show(a.name);
        function show(data) {
            jQuery("body").append('<li>' + data + '</li>')
        }
    
    </script>
    </body>
    </html>

    分离构造器

    http://www.imooc.com/code/3398

    var $$ = ajQuery = function(selector) {
            this.selector = selector;
            return this
        }
        ajQuery.fn = ajQuery.prototype = {
            selectorName:function(){
                return this.selector;
            },
            constructor: ajQuery
        }
        var a = new $$('aaa');  //实例化
        console.log(a);
        var name =  a.selectorName();//aaa //得到选择器名字
        console.log(name);
    改进,去掉new
    ar $$ = ajQuery = function(selector) {
            console.log(this);
            if(!(this instanceof ajQuery)){
                return new ajQuery(selector);
            }
            this.selector = selector;
            return this;
        }
        ajQuery.fn = ajQuery.prototype = {
            selectorName:function(){
                return this.selector;
            },
            constructor: ajQuery
        }
        var a = $$('aaa');  //实例化
        console.log(a);
        var name =  a.selectorName();//aaa //得到选择器名字
        console.log(name);
  • 相关阅读:
    PyTorch深度学习:60分钟入门(Translation)
    强化学习入门·
    leetcode 697. Degree of an Array
    耶路撒冷圣城起源笔记
    仓储机器人路径规划笔记
    算术编码原理
    ★房贷计算器 APP
    Sublime
    CocoaPods
    Xcode 6 创建 Objective-C category
  • 原文地址:https://www.cnblogs.com/darr/p/5104675.html
Copyright © 2011-2022 走看看