How can I pass methods in javascript?

Posted by peterjwest on Stack Overflow See other posts from Stack Overflow or by peterjwest
Published on 2010-06-06T10:50:35Z Indexed on 2010/06/06 10:52 UTC
Read the original article Hit count: 349

I often need to pass methods from objects into other objects. However I usually want the method to be attached to the original object (by attached I mean 'this' should refer to the original object). I know a few ways to do this:

a) In the object constructor: ObjectA = function() { var that = this; var method = function(a,b,c) { that.abc = a+b+c }}

b) In objectA which has been passed objectB: objectB.assign(function(a,b,c) { that.method(a,b,c) })

c) Outside both objects: objectB.assign(function(a,b,c) { objectA.method(a,b,c) })

I want to know if there is a simpler way to pass methods attached to their original objects.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about functional-programming