JavaScript work must Know (v) use of eval

Source: Internet
Author: User

Eval

Eval (parse) Parse: Inside with the argument string, we know that executing JavaScript will compile execution,

to change the value of a global variable:

      

        var x = 2; Defines the global variable        alert (x),//2        var g = eval ("x=3"), and//eval resolves x alert (x) based on the current context        ;//3

referencing eval at the global scope, changing the value of the global scope without changing the value of the local scope

        var g = eval; Global reference eval        var x = "global";//define global variable        (function f () {            var x = "local";            G ("x+= ' changed '");            alert (x);//local variable locally        });//iife immediately executes        the expression alert (x);//The value of the global variable globalchanged

  Reference changes the value of a local variable within a local scope without changing the value of the global variable

       var g = eval; Global reference eval        var x = "global";//define global variable        (function f () {            var x = "local";            Eval ("x+= ' changed '");            alert (x);//local variable localchanged        });//iife immediately executes an expression        alert (x);//global variable Value
Summary

Eval execution will determine whether the variable variables are local or global depending on the context, so using the Eval function is the key to seeing the scope of the reference eval!

JavaScript work must Know (v) use of eval

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.