如何使用链式函数和默认函数正确返回
How to return properly with both chained and a default function
我通过搜索看到的教程会给我留下一种不理想的格式,比如
main.default('word');
main.default('word').chain();
我想在哪里做
main('word');
main('word').chain();
这段代码只是为了一个console.log()示例,看看我是否能把这个词贯彻到底。日志"单词"或日志"链式单词"
如果我return this;
,那么我可以连锁:
var main = function(input){
this.input = input;
this.chain = function(){
this.chained='chained '+this.input;
return this.chained;
}
this.default = function(){
return this.input;
}
return this;
};
console.log(main('word').chain()); //'chained word'
但是,根据我所读到的内容,我无法执行console.log(main('word').chain().chain());
,因为链式函数不返回this
,而是返回this.chained
然后,如果我在main
函数中执行return this.default();
,我希望main
执行的默认操作将发生
var main = function(input) {
this.input = input;
this.chain = function() {
this.chained = 'out '+this.input;
return this.chained;
}
this.default = function() {
return this.input;
}
return this.default();
};
console.log(main('word')); //'word'
我也一直在查看jquerys的核心文件,看看我是否能找出他们是如何使用jQuery函数做到这一点的,但我真的看不出那里发生了什么。
这就是您想要归档的内容吗:
main('word').chain().chain().chain().getInput();
// resulting in 'chained chained chained word'
尝试始终返回this,而不是像问题代码中那样返回字符串。现在您可以修改内部变量,但需要获取。。。()功能从外部访问它们。
var main = function(input) {
this.input = input;
this.chain = function() {
this.input = 'chained ' + this.input;
return this;
}
this.getInput = function() {
return this.input;
}
return this;
};
var valueString = main('word').chain().chain().chain().getInput(); //'chained chained chained word'
document.querySelector('#result').value = valueString;
input {
width: 100%
}
result:
<input id="result" />
我在这里找到了我需要的东西:http://blog.buymeasoda.com/creating-a-jquery-like-chaining-api/
万一那个网站消失了。。。
实现我们自己的链接API为了简化事情,如果我们移除一些层,下面是我们如何实现jQuery构造函数提供了一个简单的链接API。
var myQuery, $; (function() { myQuery = $ = function(selector) { return new MyQuery(selector); }; var MyQuery = function(selector) { // Lets make a really simplistic selector implementation for demo purposes var nodes = document.getElementsByTagName(selector); for (var i = 0; i < nodes.length; i++) { this[i] = nodes[i]; } this.length = nodes.length; return this; }; // Expose the prototype object via myQuery.fn so methods can be added later myQuery.fn = MyQuery.prototype = { // API Methods hide: function() { for (var i = 0; i < this.length; i++) { this[i].style.display = 'none'; } return this; }, remove: function() { for (var i = 0; i < this.length; i++) { this[i].parentNode.removeChild(this[i]); } return this; } // More methods here, each using 'return this', to enable chaining }; }()); // Example usage $('p').hide().remove(); // Add a plugin $.fn.red = function() { for (var i = 0; i < this.length; i++) { this[i].style.color = 'red'; } return this; } $('li').red();
相关文章:
- ES6构造函数返回基类的实例
- 从函数返回角度承诺
- 如何从jquery函数返回变量
- 根据是否解析了 Promise 从函数返回值
- Javascript函数返回未定义
- 如果函数返回True,则显示Javascript按钮
- Google Sheet自定义函数返回0
- 从Ajax函数返回值
- 使用for循环从Javascript中的函数返回多个值
- 谷歌地图:函数返回未定义的值在console.log中运行良好
- 从函数中的函数返回数组时出错
- 如何从嵌套的API函数返回值
- 从Mongoose结果匿名函数返回父函数
- 函数返回错误'令牌{'
- Jquery函数返回订单问题
- Mocha/Chai测试链接到函数返回断言错误
- 从异步函数返回值
- 函数返回后更新变量
- 我如何才能继续'如果'语句来比较作为参数的多个函数返回值
- 将外部函数返回的id传递给内部函数