1
fnOriginal = function(){
  console.log("hello");
  return 10;
};

var fun = new Function("fnOriginal()");
console.log(fun()); // prints undefined.

console.log(fnOriginal()); // prints 10

How would I make fun() return and print 10 like fnOriginal()?

2 Answers 2

2

Obviously,

var fun = new Function("return fnOriginal()");

new Function("code") is the same as function() { code }. If the code's missing a return statement, the function won't return anything.

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

Comments

0

Use following

fnOriginal = function(){
  console.log("hello");
  return 10;
};

var fun = new Function("return fnOriginal();");
alert(fun());

It will return 10.

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.