开发者

when to use Function in javascript?

开发者 https://www.devze.com 2022-12-19 00:42 出处:网络
I have been writing javascript for one or two months , I never used Function keyword , like Function.method(); when I define function , I simply de开发者_如何转开发fine it as :

I have been writing javascript for one or two months , I never used Function keyword , like Function.method(); when I define function , I simply de开发者_如何转开发fine it as :

function fooBar () {};

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


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...


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.


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消