// {
// let a = 1
// var b = 2
// }
// console.log(b)
// console.log(a)
// let a
//暂时性死区
// let tmp =123
// if(true){
// console.log(tmp)
// typeof tmp //意味着typeof不再是百分百安全的操作
// let tmp = 222
// console.log(tmp)
// }
//不容许重复声明
// {
// let a =1
// var a =2
// }
//常量必须要赋值
const abc = null
//块级作用域内层不影响外层
{
let a = 123
{
let a = 234
}
//console.log(a)
}
//块级作用域的出现,使得立即执行函数不在必须
//IIFE写法
(function() {
})()
//块级作用域写法
{
}
if(true) {
function fn() {console.log('fn提升')}
}
try{
function fn1() {console.log('fn1提升')}
}catch(e) {
}
// fn()
// fn1()
//函数在块级作用域中声明,在块级作用域外不可引用
function f() { console.log('I am outside!'); }
(function () {
if (false) {
// 重复声明一次函数f
function f() { console.log('I am inside!'); }
}
//f();
}());
// 等同于
function f() { console.log('I am outside!'); }
(function () {
var f= undefined
if (false) {
// 重复声明一次函数f
function f() { console.log('I am inside!'); }
}
//f();
}());
{
let a = 'secret';
function f() {
return a;
}
//console.log(f())
}
//提案:块级作用域的返回值
// let x = do {
// let x = 123
// return x
// }
//彻底冻结对象方法
function freezeM(obj) {
Object.freeze(obj)
//
Object.keys(obj).forEach((key)=> {
if(typeof obj[key] === 'object') {
freezeM(obj[key])
}
})
}
//顶层对象属性
gg =1 //window.gg = 1
function fgg(){} //window.fgg = function fgg(){}
//console.log(gg)
let g =123// let const class import
//console.log(window.g) //undefined
function log(...args) {
return console.log(...args)
}
// 变量的结构与赋值
let [a,b] = [1,2]
//log(a,b)
let [x,y,z] = new Set([1,2,3])
//Generator 写一个斐波那契额数列
function *ff() {
let x =0
let y =1
while(true) {
//log(y)
yield y
x = y
y = x+y
}
}
let gen = ff()
for(let i=0;i<10;i++) {
gen.next()
}
//
const {a:bbb=333} = {a:undefined}
//log(bbb)
//
let arrayList = [1,2,3,4,5]
const {0:start,[arrayList.length-1]:end} = arrayList
// log(start)
// log(end)
//
let xxx
({xxx} = {xxx:1})
//log(xxx)
//'length'.forEach((s)=> {log(s)}) //"length".forEach is not a function
Array.prototype.forEach.call('length',(s)=> {
//log(s)
})
var words = ['one', 'two', 'three', 'four'];
words.forEach(function(word) {
//console.log(word);
if (word === 'two') {
words.shift();
}
});
console