javascript - Interrupting prototype function propagation -


firing events on every form field, start data control using prototype functions. if finds type of field of current object id obj.id among array contents listdatatypes, proceeds further regex controls (of course, there php overlayer no ajax i'd avoid recoding everything).

this works charm, i'm wondering how interrupt propagation of array needle search (eg called prototype 2) once needle has been found.

here code principle :

// proto 1 if (!string.prototype.contains) {     string.prototype.contains = function(stack) {         return this.indexof(stack) != -1;     }; }  // proto 2 if (!array.prototype.foreach) {   array.prototype.foreach = function(my_callback)   {     var len = this.length;     if (typeof my_callback != "function")       throw new typeerror();      var thisp = arguments[1];     (var = 0; < len; i++)     {       if (i in this)           my_callback.call(thisp, this[i], i, this);     }   }; }  // ... main code abstract            function str_search(element, index, array){          // store found item ... , stop search propagation              if (element.contains(obj.id) )                 stored_id = obj.id;           }           listdatatypes.foreach(str_search) ;   // ... 

thx

if you're asking if foreach loop can broken, answer no.

you set flag in function pass disable main part of code, that's it. loop continue until end.

if want break loop, use traditional for loop instead, or write custom foreach type of method can broken based on return value of function argument.


edit:

here's while breaks when return false.

array.prototype.while = function(my_callback) {     var len = this.length;     if (typeof my_callback != "function") throw new typeerror();      var thisp = arguments[1];     (var = 0; < len; i++) {         if (i in this) {             var ret = my_callback.call(thisp, this[i], i, this);             if (ret === false) {                 break;             }         }     } }; 

your code:

function str_search(element, index, array){      if (element.contains(obj.id) ) {         stored_id = obj.id;         return false;      } } listdatatypes.while( str_search ); 

Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -