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)

不言
Release: 2018-08-29 16:23:12
Original
3997 people have browsed it

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
Copy after login

1,

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

2,

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template