Introduction to DSL meta programming in JavaScript _javascript tips

Source: Internet
Author: User
Tags javascript eval

When looking at JavaScript template source code, found that there is a very interesting use to generate functions, think this is not metaprogramming it?

JavaScript Meta Programming

Copy Code code as follows:

Metaprogramming (metaprogramming) refers to the writing of a class of computer programs that write or manipulate other programs (or themselves) as their data, or work at runtime to complete portions that should have been done at compile time.

JavaScript Eval

Copy Code code as follows:

The eval () function computes a string and executes the JavaScript code in it.

You can use the following:
Copy Code code as follows:

Eval ("X=10;y=20;document.write (x*y)")

Of course, this is only used to carry out a function, but the goods cost-effective, but also easy to make mistakes.
Copy Code code as follows:

The Eval function should be avoided as much as possible.

So the better way is to use the new Function ()

A big difference between using the new function () and eval () is that Eval is more than just a function,

Copy Code code as follows:

Eval () evaluates a string as a JavaScript expression in the current execution scope and can access local variables. The New function () resolves the JavaScript code that is stored in a string that converts to a function object and then can be invoked. Because the code runs in a separate scope, local variables cannot be accessed.

This means that eval () interferes with the scope of the current function.

JavaScript New Function ()

function constructor to create a new function object. In JavaScript, each function is actually an object of functions. The function objects that are generated by using the functions constructor are parsed when they are created. This is less efficient than using a function declaration and calling it in your code, because functions declared using function statements are parsed with other statements.

The New Function () is relatively inefficient in comparison, and this is what we can predict in the current situation.

A simple example is shown below:

Copy Code code as follows:

var add = new Function ([' X ', ' y '], ' return x + y ');

The new function () resolves the string to a function ... And then we can do it through apply.
Copy Code code as follows:

Function.apply (null, args)

And that's what I see in the JavaScript Template:
Copy Code code as follows:

New Function (
Tmpl.arg + ', Tmpl ',
"var _e=tmpl.encode" + Tmpl.helper + ", _s= '" +
Str.replace (Tmpl.regexp, Tmpl.func) +
"'; return _s;"
);

Of course we have other ways.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.