0

I know how to apply a method whole array with myArray.map( fn ).

Although it looks weird and awkward, I want to run each objects method taking parameter as it self. Like

function MyObject(i) {
 this.i = i;
 this.internalMethod = function () {
     return this.i * 100
   }
}

function externalMethod(object){
   return object.i * 100
}

var objects = [new MyObject(3), new MyObject(-1), new MyObject(5)]
objects.map ( externalMethod ) // This works but
/// [300, -100, 500]

objects.map ( arrayElements.internalMethod )

1 Answer 1

1

Wrap the internal function in an anonymous function and pass it the currently mapped object.

JSFiddle

var newObj = objects.map(function(curr) {
    return curr.internalMethod()
});
console.log(newObj)
// [300, -100, 500]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.