让我们想象这样的功能:
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)
我该怎么做?
我试图在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();
非常感谢!
我的函数根据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($) {
$.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)