2

I have been writing javascript for one or two months , I never used Function keyword , like Function.method(); when I define function , I simply define it as :

function fooBar () {};

Can you give me an example when using Function is more convenient ?

3 Answers 3

2

You shouldn't use the Function constructor so often, it basically uses code evaluation to build a function.

It requires string arguments, being the last argument the function body, and the previous ones will be the arguments of the new function itself, for example:

var add = new Function("a", "b", "return a + b;");
add(5,5) == 10;

When you should use it?

As I said not so often, I personally try to avoid them since they use code evaluation.

A thing to note is that no closures are created when functions are built in this way, which can be a good thing for some performance circumstances, for example to shorten the process of identifier resolution, but you should use them with care...

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

Comments

1

Every function in JavaScript is actually a Function object.

Read Function

Function objects created with the Function constructor are parsed when the function is created. This is less efficient than declaring a function and calling it within your code, because functions declared with the function statement are parsed with the rest of the code.

Comments

0
function view(){ document.getElementById('one').innerHTML="New Text here"; }

this function can you call from either keyboard events of mouse events

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.