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:
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
Post a Comment