user1315603
user1315603

Reputation: 11

How do I pass a method through another method without it being called, and pass a variable object into it?

Sorry for my last question. This is the question with better formatting.

I have a method that I am passing a method through :

method("my_passed_method()")

function method(passed_method){
    eval(passed_method.replace('()', + '(' + obj + ')' );
} 

But this returns :

SyntaxError: missing ] after element list

I'm assuming this is just some simple JSON syntactical error.

Upvotes: 1

Views: 81

Answers (4)

Benjie
Benjie

Reputation: 7946

eval is evil; or so they say. You don't need to pass the function as a string you can just pass the function itself and then use Function.call or Function.apply to pass the argument (or just call it directly):

method(my_passed_method);
function method(passed_method) {
  passed_method(obj);
  // OR: passed_method.call(window,obj);
}

The circumstances where eval are necessary are very rare.

Note that your code will evaluate obj as javascript, so the outputs may differ.

Upvotes: 0

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

In Javascript functions are considered objects. You may pass them as parameters to other functions, as demonstrated below.

function something(x){
alert(x);
}

function pass(func){
  pass2(func);
}

function pass2(func){
  func("hello");
}

pass(something); //alerts hello

Demo: http://jsfiddle.net/wBvA2/

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074118

I think you probably want this:

method(my_passed_method)

function method(passed_method){
    passed_method(obj);
} 

Note that when you're calling method, you're passing in a function reference, not a string, and you're not calling my_passed_method (there are no () after it), you're just referring to it.

Within method, you call the function via the variable passed_method, because that variable contains a function reference.

In JavaScript, functions are first class objects. Variables can refer to them, you can pass them around, etc.

Here's a complete, self-contained example:

// The function we'll pass in
function functionToPass(arg) {
    display("functionToPass's arg is: " + arg);
}

// The function we pass it into
function functionReceivingIt(func) {
    func("foo");
}

// Do it
functionReceivingIt(functionToPass);

Live copy | source

Upvotes: 4

bigblind
bigblind

Reputation: 12867

The name of a method, is at the same time, your reference to that method object. The parentheses, optionally with parameters in between them, make javascript call that method. This means your code can be rewritten to:

method(my_passed_method)

function method(passed_method){
    passed_method();
} 

So, what's going on here? When you pass in the name my_passed_method, the javascript engine will look what that name maps to, and find that it maps to a functio object. Than, inside the function call of method, that object is assigned to the parameter name `passed_method. Than, putting parentheses after this name will make javascript try to execute that object, which is indeed possible because this object is a function, and just like any other type, functions are what we call first class citezens. You can treat the just like any other value by assigning them to variables and passing them around.

Upvotes: 2

Related Questions