Effective JavaScript Item 27 encapsulates code using closures instead of strings

Source: Internet
Author: User
Tags benchmark closure

this series as effective JavaScript 's reading notes.

for code encapsulation, the JavaScript There are two ways to do that. The first is to use function, the second is to use the eval () function, and the string argument passed into the function can be a piece of code.

when you are hesitant about which method to use, use the function . Because an important disadvantage of using strings is that the passed-in string is not a closure, and the function can represent a closure. The characteristics of closures are described in Item one .

Here's an example of using strings to encapsulate code:


function repeat (n, action) {for (var i = 0; i < n; i++) {eval (action);}}

incoming to Eval of the Action is a string that represents the logic that needs to be executed, and the variables that appear in the string are interpreted as global variables, in most environments window the variable on the object.

For example, in the following examples:


var start = [], end = [], timings = [];repeat (+), "Start.push (Date.now ()); f (); End.push (Date.now ()) "); for (var i = 0, n = start.length; i < n; i++) {timings[i] = End[i]-start[i];}

Repeat the second argument to the function is a piece of code that uses two variables Start and the End .

they are referring to global variables Start and the End . When the above code is not in any function, it will work, if you put them in the function:


Function benchmark () {var start = [], end = [], timings = [];repeat (+), "Start.push (Date.now ()); f (); End.push (Date.now ()) "); for (var i = 0, n = start.length; i < n; i++) {timings[i] = End[i]-start[i];} return timings;}

at this time, Start and the End variables still refer to global variables Start and the End . If there is no one of these two global variables at run time, the referenceerror exception is reported, which is the best result. If you do not happen to have these two identically named properties in the global object, the result of the program's operation is unpredictable.

A better way to encapsulate code is to use function :


function repeat (n, action) {for (var i = 0; i < n; i++) {action ();}}

At this point, you can apply the above function to the inside of any function:


Function benchmark () {var start = [], end = [], timings = [];repeat (), function () {Start.push (Date.now ()); F (); End.push ( Date.now ());}); for (var i = 0, n = start.length; i < n; i++) {timings[i] = End[i]-start[i];} return timings;}

Start and the End can refer to Benchmark function internally defined by the Start and the End Array. This is due to the nature of closures.

The other one does not use Eval The reason for this is: in JavaScript execution engine, it is often difficult to optimize for code encapsulated in string form. Because a string might be generated dynamically, the compiler / interpreter cannot optimize it by knowing its specific information before execution.

Summarize:

    1. If you do need to use strings to encapsulate a piece of code, make sure that you do not rely on any non-global variables in it
    2. The use of closures for code encapsulation instead of strings is preferred.

Effective JavaScript Item 27 encapsulates code using closures instead of strings

Related Article

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.