继续上次的jq基本框架,现在我们再来扩展一下。
Php链式调用的关键是使用_call函数,而js使用链式调用的关键是返回this执行。比如上面的代码,我们再来添加一个方法:
(function(){
var test=function(){
return new test.prototype.init();
}
test.prototype={
init:function(){
return this;
},
name:function(){
alert(123)
return this;
},
pwd:function(){
alert(456)
}
}
test.prototype.init.prototype=test.prototype;
$=test;
}())
因为name返回的是this,所以再使用.pwd(),相当与使用$().pwd()
这一点,跟c++中cin,cout中返回的是输出输入流本身一样。