js进阶之作用域、函数进阶以及解构赋值
    		       		warning:
    		            这篇文章距离上次修改已过435天,其中的内容可能已经有所变动。
    		        
        		                
                
// 作用域
function scopeTest() {
  let a = 20;
  const b = 30;
  var c = 40;
  
  // 块级作用域
  if (true) {
    let d = 50;
    const e = 60;
    var f = 70;
  }
  
  console.log(a); // 20
  console.log(b); // 30
  console.log(c); // 40
  // console.log(d); // 报错,d 在此作用域内不存在
}
scopeTest();
 
// 函数进阶
function funcAdvanced(x, y) {
  // 默认参数
  x = x || 100;
  y = y || 200;
  console.log(x, y);
}
funcAdvanced(); // 输出 100 200
 
// 箭头函数
const arrowFunc = (x, y) => {
  return x + y;
};
console.log(arrowFunc(5, 6)); // 输出 11
 
// 解构赋值
const obj = { x: 100, y: 200 };
const { x, y } = obj;
console.log(x, y); // 输出 100 200
 
const arr = [1, 2, 3];
const [a, b, c] = arr;
console.log(a, b, c); // 输出 1 2 3以上代码示例涵盖了JavaScript中的作用域、函数进阶(包括默认参数、箭头函数)以及解构赋值的基本使用。通过这些示例,开发者可以更好地理解和应用这些重要的概念。
评论已关闭