2

I want to add a function to some object (in a form of a variable) and execute it when i need too.
How to do this?
Thanks.

0

3 Answers 3

5
obj.doSomething = function()
{
  console.log('done');
}

obj.doSomething();

This won't affect any of obj's existing fields or methods (the obvious exception being if there's already a doSomething).

Sign up to request clarification or add additional context in comments.

Comments

5
var myFunc = function() { ... };
var myObj = { func: myFunc };

myObj.func();

You can also skip the myFunc temporary variable if you want to.

Comments

1

Pretty vague, but here you go:

var obj = {};
obj.foo = function() {
    return "baz";
};

// code...

obj.foo();

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.