jQuery插件与链式方法

时间:2017-11-30 19:06:44

标签: jquery

我正在构建一个技术分析插件,我正在寻找一些指导。

我想做这样的事情:

var ohlcList; //a list of stock data points, open high low close

var bars = $().indicators(ohlcList)
                .MA(50)
                .MA(100)
                .MACD(12, 26, 9);

插件将循环通过ohlcList将指标MA(50),MA(100)和MACD(12,26,9)应用于它。结果如下:

bars = [{ Open: 2, High: 3, Low: 1, Close: 2, MA_50: 2, MA_100: 3, MACD_12_26_9: 4 }, {...}]

有关插件外观的任何想法?

1 个答案:

答案 0 :(得分:0)

您可以使用jQuery链接函数,并将其与return this;结合使用。这样下一个函数就知道你还在引用原始对象。

$.fn.example(someNumber){
    // `this` now is the object you applies it too

    // Very basic example of what you can do now:
    this.counter = this.counter+1 || 0; // keep an internal counter
    this[someNumber] = this.counter;
    // Do things with it as you please

    return this; // now the next method call knows what you're talking about.
}