I have implemented for-each as in jQuery, this is the code below,var $ = (function(){   var obj = {};   return obj;})();$.each = function(collection,fn){    if(collection.constructor.name === 'Array'){        for(var i = 0; i < collection.length; i++){            fn.call(this,collection[i]);        }       }else{        for(var prop in collection){            fn.call(this,collection[prop]);        }    }};$.each([1,2,3,7,8],function(num){    console.log(num * 2);});$.each(['A','B','C'],function(char){    console.log(char);});$.each({     name : 'Rahul Shivsharan',     department : 'Computer Science' },function(value){       console.log(value);});The output of the code is2461416 ABCRahul ShivsharanComputer SciencePlease review my jQuery style for-each implementation, and let me know your feedback
My for-each implementation
javascript;jquery;array
The non-array iterator would normally be written as:for (var prop in collection) {  if (collection.hasOwnProperty(prop)) {    fn.call(this,collection[prop]);  } }to avoid iterating over non-direct properties. See     https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty