javascript - Can I call all methods of an object? -


let's have object:

var shapes = {      addtriangle: function(color){          console.log("added " + color + " triangle");      },      addsquare: function(color){          console.log("added " + color + " square");      } } 

this object have methods changed , updated frequently, methods running sequentially. there way automatically , run methods, ease maintainability? like:

function runshapes(color){     shapenames = object.getownpropertynames(shapes);     for(i = 0; < shapenames.length; i++){         shapenames[i].apply(color);     } } 

this gives `shapenames[i].apply not function'. bonus points telling me how functional programming, instead of loop :)

codepen:

http://codepen.io/anon/pen/mjxvye

first off mistake... iterating through property names , not properties.

shapenames[i].apply(color); 

should be:

shapes[shapenames[i]](color); 

a more functional version might this:

function runshapes(color){     object.keys(shapes).foreach(function(each){         shapes[each](color);     }); } 

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -