zoukankan      html  css  js  c++  java
  • js实现方法的链式调用

    假如这里有三个方法:
    person.unmerried();
    person.process();
    person.married();
    在jQuery中通常的写法是:person.unmerried().process().married();
    而在js中要实现链式调用,只需在类中的每个方法中通过this关键字返回对象实例的引用。

    function Person(){};
    Person.prototype.status =false;
    Person.prototype.married =function(){
    this.status = true;
    return this;
    };
    Person.prototype.unmerried = function(){
    this.status = false;
    return this;
    };
    Person.prototype.process = function(){
    alert("I'm in love");
    return this;
    }
    var bob = new Person();
    bob.unmerried().process().married();
  • 相关阅读:
    HDU 4005 The war
    #undef
    [转载] #define new DEBUG_NEW
    [转载]常用正则表达式
    [百科]
    [转载]
    [转载]
    [转载]
    [百科]
    [转载]VC6中的文件后缀
  • 原文地址:https://www.cnblogs.com/littlewriter/p/6218011.html
Copyright © 2011-2022 走看看