javascript - Is there a simple way to quickly pass the entire contents of a given array into an `Array.prototype` method variable without using parameters? -
context
i created array docket keep track of coordinates user clicks on canvas space. during main program loop array scanned draw function selected pixels can seen. originally, inside of event listener, using push( ) method realized wanted way sort of toggle pixels.
code description
so added method poke( ) array.prototype, seen below, allows me push whole docket array local array param.array , assign trigger coordinate local variable param.entry. entry pushed array , array processed main poke( ) loop ensure there no duplicate values. if match found, both elements annihilated , param.array returned top, shrinking docket 1; if no matches found no elements annihilated , param.array returned top, expanding docket 1.
main issue: example 1
anyway, method written, must called thusly:
docket.poke( docket, e.key ); note: simplicity have used keyboard key values.
array.prototype.poke = function( a, b ) { var bool = { }, = { }, param = { }; param.array = a; param.entry = b; // param.array.push( param.entry ); i.len = param.array.length; i.end = i.len - 1; // ( i.cur = 0; i.cur < i.len; i.cur++ ) { bool.match = param.array[ i.cur ] == param.array[ i.end ]; bool.nself = !( i.cur == i.end ); // if ( bool.match && bool.nself ) { param.array.splice( i.end, 1 ); param.array.splice( i.cur, 1 ); // i.end -= 2; i.len -= 2; } } // return param.array; } this seems little redundant, offers 2 critical advantages. first readability , aesthetic. being able visibly pass off contents of docket local array processing , visibly return results top think helpful comprehension. second, both example , next use sort of confusing truth test filter out false positives on duplicate value detection. example doesn't have though. rewritten compare each element in param.array param.entry using tight, no nonsense loop.
main issue: example 2
docket.poke( e.key ); less redundant , more desired approach. code.
array.prototype.poke = function( ) { var bool = { }, entry = a, = { }; // this.push( entry ); i.len = this.length; i.end = i.len - 1; // ( i.cur = 0; i.cur < i.len; i.cur++ ) { bool.match = this[ i.cur ] == this[ i.end ]; bool.nself = !( i.cur == i.end ); // if ( bool.match && bool.nself ) { this.splice( i.end, 1 ); this.splice( i.cur, 1 ); // i.end -= 2; i.len -= 2; } } } as can see, eliminates the redundancy in call, sacrifices readability of method , more importantly opportunity slim code using simple comparison mentioned above.
so i'm wondering if there less obvious way i've missed allow me pass full contents of array local variable without having first pass them in parameter of own method.
any ideas?
there no reason define method on prototype if going pass array argument. plain function fine that.
the second version of code has indeed advantage can apply method given array instead of passing array function.
the code simplified if:
- you add element after have determined not yet occur in array
- you use
indexof:
array.prototype.toggle = function(value) { var index = this.indexof(value); if (index > -1) { this.splice(index, 1); } else { this.push(value); } } var = [4,2,5,8]; a.toggle(2); console.log(a.join()); a.toggle(2); console.log(a.join()); nb: find name toggle more telling poke.
consider power of set: find existing member in constant time (while array implementation needs linear time), , able remove in constant time. if open using else array this, go set.
set.prototype.toggle = function(value) { if (!this.delete(value)) this.add(value); } var = new set([4,2,5,8]); a.toggle(2); console.log([...a].join()); a.toggle(2); console.log([...a].join());
Comments
Post a Comment