27.链模式

62 阅读1分钟
 // var A = function() {}
    // A.prototype = {
    //     length: 2,
    //     size: function() {
    //         return this.length
    //     }
    // }
    // var a = new A() 
    // a.size()


    var A = function(id, context) {
        return new A.fn.init(id, context)
    }
    A.fn = A.prototype = {
        constructor: A, 
        init: function(id, context) {
            this.context = context || document
            this.id = id
            this[0] = document.getElementById(id)
            this.length = 1
            return this
        },
        length: 2,
        size: function() {
            return this.length
        },
        push: [].push,
        sort: [].sort,
        splice: [].splice
    }
    A.fn.init.prototype = A.fn 
    A('demo','helloWorld')
    /*
    function A() {
        return  new A.fn.init()
    }
    A.fn = A.prototype = {
        length,
        size,
        init: function() {
            ///...
            return this
        }
    }
    A() 
    {
        length,
        size,
        init
    }
    */