JavaScript对象方法链接:有用吗?

编程入门 行业动态 更新时间:2024-10-26 01:32:06
本文介绍了JavaScript对象方法链接:有用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以...在JavaScript中乱搞一个对我来说很新的想法,让Object的方法返回它们是方法的Object;这导致了可链接性。我的问题是:这怎么有用?我把它扔在一起测试基本工作:

So... messing around in JavaScript with an idea that's new to me, having methods of an Object return the Object of which they are methods; this then leads to chainability. My question, then: how can this be useful? I threw this together to test the fundamental workings:

<script> MathChain = function() { this.pass = function() { this.multiply = eval(arguments.join('*')); this.add = eval(arguments.join('+')); return this; } } m = new MathChain().pass(5, 10, 20).multiply; // 1000 a = new MathChain().pass(5, 10, 20).add; // 35 </script>

这显然不是一个使用这个概念的恶毒有效的实例,所以你能指点我确实做得恰到好处(除了jQuery之外)?

That's obviously not a viciously efficient instance in which one would use this concept, so could you point me to something that does do so properly (aside from jQuery, please)?

推荐答案

嗯,这里有一个不太现实的世界例如,但我想你会明白这个想法。如果允许您对对象执行许多不同的操作,并提供方便。

Well, here is a not very real-world applicable example, but I think you'll get the idea. If allows you to do a number of different operations on an object, and provides convenience.

var truck = function() { this.turnLeft = function { // turn left return this; } this.turnRight = function { // turn right return this; } this.goReallyFast = function { // go fast! return this; } }; // My get-away plan var myTruck = new truck(); myTruck.turnLeft().turnRight().goReallyFast();

更多推荐

JavaScript对象方法链接:有用吗?

本文发布于:2023-05-31 22:05:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/400233.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有用吗   对象   链接   方法   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!