介绍下作用域链

2020-03-08  本文已影响0人  李幸娟

介绍一下JavaScript作用域链?

  1. 什么是作用域链

const color = 'red'
const function = sayColor(){
  const innerColor = 'blue'
  console.log(color)
}
sayColor()  // 'red'

  1. 延长作用域链
function basicUrl(){
  var qs = 'haha';
  with(location){
    return url = href + qs
  }
  return url;
}
basicUrl()
// "http://localhost:4000/Blog/haha"
function compare(value1, value2) {
  if (value1 > value2) {
    return 1;
  } else if (value1 < value2) {
    return -1;
  } else {
    return 0;
  }
}
const result = compare(2, 3);

上一篇 下一篇

猜你喜欢

热点阅读