InvokeArgsPoint.prototype.exec = function(item) { item[this.name].apply(item[this.name], this.args); } InvokeArgsPoint.prototype.run = function(a) { var i = 0, l = a.length, n = this.name, args = this.args; while(i < l) { var m = a[i++][n]; m.apply(m, args); } }
var targetID = null;var maxValue = 0;for (unitID in this.damageTaken) { if (this.damageTaken[unitID] > maxValue) { targetID = unitID; maxValue = this.damageTaken[unitID]; }}
var item = Link(this.damageTaken).max(function(item) { return item.value; });item.id // the answer you are looking for.
var item = Link(this.damageTaken, { hash: true }).max(function(item) { return item.value; });item.key // the answer you are looking for. Notice it's called key now.
// updating health of all poisoned players in an associated array: ["Jim" = new Player(), "Bob" = new Player()];Link(this.players, { hash: true }).pluck("value").has("status", "poison").invoke("hurt", 5, "green");// or you can do this with a normal array:Link(this.players).has("status", "poison").invoke("hurt", 5, "green");
HeadlessHorseAI.prototype.onUnitDamaged = function(unit, amount, tags, attacker){ if (unit === this.unit && attacker !== null) { if (!(attacker.id in this.damageTaken)) { this.damageTaken[attacker.id] = 0; } this.damageTaken[attacker.id] += amount; }};
var combos = Link(Link(this.combos) .where(function(combo) { return combo.phase <= this.phase; }.bind(this)) .sort(function(a, b) { return b.rating - a.rating })) .random(2);
// old way:var items = Link(array).filterBy("property", value).toArray();// new way:function resolve(item) { return item.property };var items = Link(array).filterBy(value, resolve).toArray();
function schema() { this.prop = Number; this.prop2 = Boolean; // ...}Link(array, schema).filterBy(a, "prop").toArray();
I don't know, I have nothing against optimization in general, but I feel this is making low-level assumptions about the way the compiler works under the hood that could potentially, depending on the specific JS library used, change out from under you and end up making things slower. Besides the fact that it makes the code more difficult to read--if I'm going to have to pass a function in anyway, I'd rather just go all-in and use a full filter() operation. I see filterBy() as a convenience, and this takes all the convenience away.
What was wrong with random, by the way?