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);
  • 相关阅读:
    C# 上传图片前判断上传图片的宽和高
    PHP调用WebService
    js判断输入字符串长度(汉字算两个字符,字母数字算一个)
    js 验证电话号 座机及手机号
    C# 微信扫码支付 回调页面
    复制文件夹及文件
    html失去获得焦点
    SQL 大数据查询如何进行优化?
    sql表内存占用情况,并进行缩放
    查询被锁的表
  • 原文地址:https://www.cnblogs.com/darr/p/5104675.html
Copyright © 2011-2022 走看看