webveuje/js/kejian/yubianyilx.md
2021-02-02 17:27:36 +08:00

1.6 KiB

js info

预编译

链接:

function f() {
    console.log(aa); // undefined
    var aa = 5;
    console.log(aa); // 5
}
f();

demo2:

var scope = 'global';
function t() {
    console.log(scope); // undefined
    var scope = 'local';
    console.log(scope); // local
}
t();
console.log(scope); // global

demo3:

console.log(b) //undefined 变量提升
var b =2; 

demo4

console.log(foo)
function foo(){
    
}
var foo =1

作用域

demo1:

function zxxFn () {
    zxxs = 2
}
var zxx = 1
console.log(window.zxx) // 1
// console.log(zxxs) 未定义
zxxFn() // 执行后zxxs成为全局变量
console.log(zxxs) // 2

var zxx = 'zxx is a good girl'
    function zxxFn () {
        console.log(zxx) 
        var zxx = 'zxx is a great girl'
        console.log(zxx)
        zxx = 'very good'
        console.log(zxx)
    }
    console.log(zxx)
    zxxFn()
    console.log(zxx)
    // 依次输出 1. zxx is a good girl 2. undefined 3. zxx is a great girl
    // 4. very good 5.zxx is a good girl
zxxAge = 'zxx is 18 years old'
function zxxFn () {
    console.log(zxxAge)
    var zxxAge = 'zxx is 8 years old'
    var zxxSub = 'zxx is 25 years old'
    console.log(zxxAge) // zxx is 8 years old
    // console.log(zxxSub) // zxxSub is not defined
    function zxxFnSub () {
        var zxxSub = 'zxx is 18 years old'
        console.log(zxxSub) // zxx is 18 years old 
        console.log(zxxAge) // zxxSub is not defined
    }
    zxxFnSub()
}
zxxFn()