javascript 链式调用

javascript 链式调用


简单的链式调用

  function person() {

            this.run = function () {
                console.log("跑了");
                return this;

            };
            this.eat = function () {
                console.log("吃了");
                return this;
            }
        }

        var p = new person();
        p.run().eat();

一个链式调用的例子

 Function.prototype.method=function (name,fn) {
            this.prototype[name] = fn;
            return this;
        };
        function _$() {};
        _$.onready = function (fn) {
            window.$ = function () {
                return new _$(arguments);
            };
            fn();
        };

        _$.method("click",function (fn) {
            fn();
        }).method("change",function (fn) {
            fn();
        }).method("load",function (fn) {
            fn();
        });

        //使用
        _$.onready(function () {
            $().change(function () {
                console.log("改变了");
            })
        });

猜你喜欢

转载自blog.csdn.net/zshsats/article/details/82896851