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);
  • 相关阅读:
    剑指OFFER 连续数组的最大和
    剑指OFFER 两个链表的第一个公共结点
    剑指OFFER 替换空格
    剑指OFFER 二叉树中和为某一值的路径
    剑指OFFER 二叉树的镜像
    剑指OFFER 从上往下打印二叉树
    剑指OFFER 删除链表中重复的结点
    剑指OFFER 数组中只出现一次的数字
    剑指OFFER 调整数组顺序使奇数位于偶数前面
    C 语言 sleep 函数
  • 原文地址:https://www.cnblogs.com/darr/p/5104675.html
Copyright © 2011-2022 走看看