Home >Web Front-end >JS Tutorial >How to convert local variables to global variables in javascript (code)

How to convert local variables to global variables in javascript (code)

不言
不言Original
2018-08-29 16:23:124000browse

The content of this article is about the method (code) of converting local variables into global variables in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

        function enjoy1(){
            var num1=100;
        }
        enjoy1();
        console.log(num1);//报错:num is not defined

1,

        function enjoy2(){
            var num2=100;
            window.num2=num2;
        }
        enjoy2();
        console.log(num2);//100

2,

         function test(){
            var age=19;//局部
            return function(){
                console.log(age);
            }
        }        var func=test();//获取局部变量,这一步不可少
        func();//19

Related recommendations:

##Global variables and local variables in ScriptCase_PHP Tutorial

Detailed explanation of JavaScript variable declaration and local variable replacement of global variable usage

The above is the detailed content of How to convert local variables to global variables in javascript (code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn