Is there any Javascript equivalent of Ruby andand?

In an attempt to make my Javascript unobtrusive, I use onLoadto add functionality to <input>and the like. With Dojo, it looks something like this:

var coolInput = dojo.byId('cool_input');
if(coolInput) {
  dojo.addOnLoad(function() {
    coolInput.onkeyup = function() { ... };
  });
}

Or roughly equivalent:

dojo.addOnLoad(function() {
  dojo.forEach(dojo.query('#cool_input'), function(elt) {
    elt.onkeyup = function() { ... };
  });
});

Has anyone written an implementation of Ruby andand so that I can do the following?

dojo.addOnLoad(function() {
  // the input onkeyup is set iff the input exists
  dojo.byId('cool_input').andand().onkeyup = function() { ... };
});

or

dojo.byId('cool_input').andand(function(elt) {
  // this function gets called with elt = the input iff it exists
  dojo.addOnLoad(function() {
    elt.onkeyup = function() { ... };
  });
});
+5
source share
7 answers

The exact syntax you want is not possible in JavaScript. The way JavaScript is executed should change quite fundamentally. For example:

var name = getUserById(id).andand().name;
//                        ^
//                        |-------------------------------
// if getUserById returns null, execution MUST stop here |
// otherwise, you'll get a "null is not an object" exception

However, JavaScript does not work. It just doesn’t.

The next line does almost what you want.

var name = (var user = getUserById(id)) ? user.name : null;

. :

// this is what you want to see
var initial = getUserById(id).andand().name.andand()[0];
// this is the best that JavaScript can do
var initial = (var name = (var user = getUserById(id)) ? user.name : null) ? name[0] : null;

. , . , , :

var name = (function() {return (var user = getUserById(id)) ? user.name : null;})();

, . ! !:)

+2

Dojo,

dojo.addOnLoad(function() {
    var coolInput = dojo.byId('cool_input');
    if(coolInput)
        coolInput.onkeyup = function() { ... };
});

DOM.

: JavaScript andand()

function andand(obj, func, args) {
    return obj && func.apply(obj, args || []);
}

dojo.addOnLoad(function() {
    andand(dojo.byId('cool_input'), function() {
        this.onkeyup = function() { ... };
    });
});

, if - ?

+3

dojo.behavior.

dojo.behavior.add({
    '#cool_input': {
        onKeyUp: function(evt) { ... }
    }
});
+2

- :

function andand(elt, f) {
  if (elt)
    return f(elt);
  return null;
}

:

andand(dojo.byId('cool_input'), function(elt) {
  // this function gets called with elt = the input iff it exists
  dojo.addOnLoad(function() {
    elt.onkeyup = function() { ... };
  });
});
+1

, JavaScript, ​​ . , id dojo.forEach(...), forEach.

JavaScript:

dojo.byId('cool_input') && dojo.byId('cool_input').whateverYouWantToDo(...);
0

dojo, javascript ( DOM) , ( , ). , andand() .

dojo.addOnLoad(function() {
  dojo.byId('cool_input').onkeyup(function(evt) { /*event handler code*/
  });
});
0

:

Array.prototype.andand = function(property, fn) {

    if (this.filter(property).length > 0) this.map(fn);
}
0

All Articles