0

deleteObjectsDependingOnX(objects, X) と呼ばれる Javascript メソッドがあります。パラメータの順序を最初にオブジェクトとして、次に X、またはその逆にするのは慣例ですか?

これは、Javascript の規則についての質問です。私は、C++ では逆のことをするのが慣習だと信じていますが、人々が Javascript で何をしているのかはわかりませんでした。

4

2 に答える 2

1

If X is a callback function, then putting it last seems more common and leads to (IMHO) easier to read code like this:

deleteObjectsDependingOnX(objects, function(o) {
    // return true if o should die, false otherwise
});

The "callback at the end" is pretty common jQuery, see $.each and $.grep for examples.

Of course, setTimeout puts the arguments in the other order so the time value can get lost:

setTimeout(function() {
    // Do a bunch of stuff and things.
}, 500);

OTOH, if you use a named function rather than an anonymous one, it looks okay:

setTimeout(doStuffAndThings, 500);

So I think the real answer is "it depends". If you're expecting anonymous functions more often than named ones, then putting the callback at the end will make for (IMHO) easier to read code.

于 2011-06-06T06:17:13.047 に答える
1

JavaScript にはそのようなことについての規約はないと思います。

于 2011-06-06T06:12:35.273 に答える