MooTools 链接
我认为函数/方法不应该返回 void,相反,它应该返回 this
。这就是为什么我惊讶地发现这不起作用:
$('buttonContainer').getElement('input').set('value', this.get('value') + ' ');
代码应该做的是找到一个 ,它是具有
idbuttonContainer
的 code> 属性值,并在其 value
属性中添加两个空格字符。不过,上述代码错误,我不得不写:
var input = $('buttonContainer').getElement('input');
input.set('value', input.get('value') + ' ');
MooTools 没有办法将这两个单独的语句链接成一个吗?与我的第一个片段类似吗?
I don't think a function/method should ever return void—instead, it should return this
. That's why I was surprised to find out that this doesn't work:
$('buttonContainer').getElement('input').set('value', this.get('value') + ' ');
What the code is suppose to do is find an <input>
that is a child of the element with id
attribute value of buttonContainer
, and add two space characters to its value
attribute. The aforeshown code errors though, and I'm forced to write:
var input = $('buttonContainer').getElement('input');
input.set('value', input.get('value') + ' ');
Doesn't MooTools have a way to chain these two seperate statements into one? Something similar to my first snippet?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MooTools 无法为每个调用的方法即时重新绑定
this
。这是不可能的。您必须了解,对链的每次调用都在同一范围内,因此
this
保持不变。 jQuery 和其他所有框架都有同样的问题。如果您想同时对一个元素执行两个操作,则必须将该对象存储在一个变量中,然后使用该变量来引用该对象,就像您在第二个示例中所做的那样:this
可以仅当作用域发生变化时才会发生变化(在 JavaScript 中,总是当您点击包含函数的大括号{}
时)。这不是 MooTools 链接的限制。这是 JavaScript 的一般工作方式。MooTools cannot rebind
this
on the fly for every method called. This would be impossible.You have to understand that every single call to your chain is in the same scope, therefore
this
remains the same. jQuery and every single other framework have the same problem. If you want to do two operations on an element at the same time, you must store the object in a variable and then use that variable to reference the object exactly like you did in your second example:this
can only change when the scope changes (which in JavaScript is always when you hit a brace{}
enclosing a function). This is not a limitation of MooTools' chaining. It's the way JavaScript in general works.你问的与链接无关。
this
没有上下文,因此您的调用失败。您不满意的解决方案是您需要为其他值/属性编写它的方式,但是对于像这样的直接更改,请这样编写:What you are asking has nothing to do with chaining.
this
has no context, so your call fails. The solution you are not happy with is the way you will need to write it for other values/attributes, but for a straight forward change like this, write it this way: