为了让我的Javascript不显眼,我使用onLoad
s来为<input>
s等添加功能。对于Dojo,这看起来是这样的:是否有Ruby的andand的Javascript等价物?
var coolInput = dojo.byId('cool_input');
if(coolInput) {
dojo.addOnLoad(function() {
coolInput.onkeyup = function() { ... };
});
}
或近似等价:
dojo.addOnLoad(function() {
dojo.forEach(dojo.query('#cool_input'), function(elt) {
elt.onkeyup = function() { ... };
});
});
有没有人写Ruby的andand,这样我可以做以下的实现?
dojo.addOnLoad(function() {
// the input's onkeyup is set iff the input exists
dojo.byId('cool_input').andand().onkeyup = function() { ... };
});
或
dojo.byId('cool_input').andand(function(elt) {
// this function gets called with elt = the input iff it exists
dojo.addOnLoad(function() {
elt.onkeyup = function() { ... };
});
});
或者使用[CoffeeScript](http://jashkenas.github.com/coffee-script/#operators):查看“存在操作符的访问器变体”?`。:) – 2011-10-29 22:51:17