Execute function/method of an object

Viewed 94

Is there a way to get something like this to work in JS:

function iterateObject(obj, f) {
    for (let prop in obj) {
        if (obj.hasOwnProperty(prop)) f(prop);
    }
}

And then apply it on an object:

let x = {a : function() {
    // do smth
}};

iterateObject(x, (prop) => {
    prop.a();
}

I'm getting an error that prop.a() is not a function but if I call x.a() there is no problem. Not super important but I'm just wondering and couldn't find an answer.

2 Answers
Related