js把局部变量变为全局变量
前提:最近在和APP之间进行交互,其中就包括APP对H5变量和方法的调用功能。 作为没有接触过此功能的我,经过一番搜索之后,熟练应用,下面是我操作,本次开发使用VUE框架,所以都是在VUE中的使用方法,当然其他项目自己更改一下还是可以用的。 第一:全局变量声明 1、未设置全局变量: < body > < button > 全局变量声明 < / button > < script > function test ( ) { var num = 10 ; console . log ( num ) } test ( ) console . log ( "num:" + num ) < / script > < / body > 输出结果: 2、设置为全局变量: < body > < button > 全局变量声明 < / button > < script > // function test(){ // var num=10; // console.log(num) // } // test() // console.log("num:"+num) function test2 ( ) { var num2 = 10 ; console . log ( num2 ) window . num2 = num2 } test2 ( ) console . log ( "num2:" +