标签: chainable

如何在JavaScript中创建可链接的函数?

让我们想象这样的功能:

function foo(x) {
    x += '+';
    return x;
}
Run Code Online (Sandbox Code Playgroud)

使用它将是:

var x, y;
x = 'Notepad';
y = foo(x);
console.log(y); // Prints 'Notepad+'.
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种方法来创建可与其他功能链接的功能.

想象一下用法:

var x, y;
x = 'Notepad';
y = x.foo().foo().toUpperCase(); // Prints 'NOTEPAD++'.
console.log(y);
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

javascript return function chain chainable

21
推荐指数
2
解决办法
1万
查看次数

PHP OOP:可链接对象?

我试图在PHP中找到关于可链接OOP对象的一个​​很好的介绍,但还没有任何好的结果.

这样的事情怎么办?

$this->className->add('1','value');
$this->className->type('string');
$this->classname->doStuff();
Run Code Online (Sandbox Code Playgroud)

甚至: $this->className->add('1','value')->type('string')->doStuff();

非常感谢!

php oop fluent-interface chainable

8
推荐指数
3
解决办法
626
查看次数

如何将其转换为可链接的jquery函数?

我的函数根据data属性返回一个过滤的(数组)项列表.

如果我可以将这个功能链接起来,我想要它:

$(document).ready(function (){
    function filterSvcType (svcType) {
        var selectedServices = $("#service-list div");
        var chose = selectedServices.filter(function() {
            return $(this).data("service-type") ==  svcType;
        });

        console.log(chose);             
    }
    filterSvcType("hosting");       
});
Run Code Online (Sandbox Code Playgroud)

我想要做的是这样称呼它:

filterSvcType("hosting").fadeOut(); 
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?

jquery function chainable

5
推荐指数
1
解决办法
1641
查看次数

如何使这个jQuery插件可链接?

谁能告诉我如何使这个jQuery插件可链接吗?该插件将文本输入限制在一个字段中,如果传入,则将剩余的文本计数返回到第二个参数。谢谢。

(function($) {

$.fn.extend({
    limit: function(limit, element) {
        var interval;
        var self=$(this);
        $(this).focus(function(){
            interval = window.setInterval(function(){
                substring(self, limit, element);
            },100)
        });
        $(this).blur(function(){
            clearInterval(interval);
            substring(self, limit, element);
        });
        substring(self, limit, element);
    }
});

function substring(self, limit, element) {
    var val = $(self).val();
    var length = val ? val.length : 0 ;
    if( length > limit ) {
        $(self).val($(self).val().substring(0,limit));
    }
    var toe = typeof element;
    if (toe!='undefined') {
        if (toe=='string') {
            $(element).html((limit-length<=0)?'0':limit-length);
        } else if (toe=='object') {
            element.html((limit-length<=0)?'0':limit-length);
        }
    }
}

})(jQuery);
Run Code Online (Sandbox Code Playgroud)

jquery plugins chainable

1
推荐指数
1
解决办法
903
查看次数