日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務,提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:52010
  • 待審:67
  • 小程序:12
  • 文章:1106242
  • 會員:784

使用 var 關鍵字聲明的變量的作用域為創(chuàng)建它們的函數(shù),或者如果在任何函數(shù)外部創(chuàng)建,則為全局對象。 let 和 const 是塊作用域的,這意味著它們只能在最近的一組花括號(函數(shù)、if-else 塊或 for 循環(huán))內訪問。

function foo() {
  // all variables are accessible within functions.
  var bar = 'bar';
  let baz = 'baz';
  const qux = 'qux';

  console.log(bar); // bar
  console.log(baz); // baz
  console.log(qux); // qux
}

console.log(bar); // referenceerror: bar is not defined
console.log(baz); // referenceerror: baz is not defined
console.log(qux); // referenceerror: qux is not defined

if (true) {
  var bar = 'bar';
  let baz = 'baz';
  const qux = 'qux';
}

// var declared variables are accessible anywhere in the function scope.
console.log(bar); // bar
// let and const defined variables are not accessible outside the block they were defined in.
console.log(baz); // referenceerror: baz is not defined
console.log(qux); // referenceerror: qux is not defined

登錄后復制

var 允許變量被提升,這意味著它們可以在聲明之前在代碼中引用。 let 和 const 不會允許這樣做,而是會拋出錯誤。

console.log(foo); // undefined

var foo = 'foo';

console.log(baz); // referenceerror: can't access lexical declaration 'baz' before initialization

let baz = 'baz';

console.log(bar); // referenceerror: can't access lexical declaration 'bar' before initialization

const bar = 'bar';

登錄后復制

用var重新聲明變量不會報錯,但let和const會報錯

var foo = 'foo';
var foo = 'bar';
console.log(foo); // "bar"

let baz = 'baz';
let baz = 'qux'; // uncaught syntaxerror: identifier 'baz' has already been declared

登錄后復制

let 和 const 的區(qū)別在于 let 允許重新分配變量的值,而 const 則不允許。

// This is fine.
let foo = 'foo';
foo = 'bar';

// This causes an exception.
const baz = 'baz';
baz = 'qux';

登錄后復制

分享到:
標簽:const var 有什么區(qū)別
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 52010

    網(wǎng)站

  • 12

    小程序

  • 1106242

    文章

  • 784

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定