第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

函數(shù)內(nèi)部的全局變量不能在外部訪(fǎng)問(wèn)

函數(shù)內(nèi)部的全局變量不能在外部訪(fǎng)問(wèn)

飲歌長(zhǎng)嘯 2023-01-06 11:01:17
如果我理解正確,在函數(shù)內(nèi)部不使用關(guān)鍵字 var 聲明變量將創(chuàng)建一個(gè)全局范圍的變量。但是當(dāng)從其容器函數(shù)外部訪(fǎng)問(wèn)變量時(shí),我得到這個(gè)“ReferenceError: oopsGlobal is not defined”。,,, // Declare the myGlobal variable below this linevar myGlobal = 10 function fun1() {  // Assign 5 to oopsGlobal Here  oopsGlobal = 5}// Only change code above this linefunction fun2() {  var output = "";  if (typeof myGlobal != "undefined") {    output += "myGlobal: " + myGlobal;  }  if (typeof oopsGlobal != "undefined") {    output += " oopsGlobal: " + oopsGlobal;  }  console.log(output);}console.log(oopsGlobal) // ReferenceError: oopsGlobal is not defined,,,
查看完整描述

2 回答

?
慕碼人8056858

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個(gè)贊

您編寫(xiě)了代碼,但從未調(diào)用過(guò)它。fun1并且fun2永遠(yuǎn)不會(huì)運(yùn)行。我在下面添加了一行,它調(diào)用了fun1()導(dǎo)致分配發(fā)生的函數(shù)。


這更像是提供演示的答案——您很可能不想實(shí)際編寫(xiě)具有全局變量或像這樣的副作用的代碼。如果您正在為瀏覽器編寫(xiě)軟件,使用window或globalThis存儲(chǔ)您的全局狀態(tài)也可能使它更清晰。


// Declare the myGlobal variable below this line

var myGlobal = 10 


function fun1() {

  // Assign 5 to oopsGlobal Here

  oopsGlobal = 5

}


fun1(); // You wrote the functions previous, but you never CALLED them.


// Only change code above this line


function fun2() {

  var output = "";

  if (typeof myGlobal != "undefined") {

    output += "myGlobal: " + myGlobal;

  }

  if (typeof oopsGlobal != "undefined") {

    output += " oopsGlobal: " + oopsGlobal;

  }

  console.log(output);

}


console.log(oopsGlobal) // ReferenceError: oopsGlobal is not defined


查看完整回答
反對(duì) 回復(fù) 2023-01-06
?
人到中年有點(diǎn)甜

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊

發(fā)生這種情況是因?yàn)槟鷮?shí)際上從未跑步過(guò)fun1()。如果你不調(diào)用一個(gè)函數(shù),里面的代碼將永遠(yuǎn)不會(huì)被執(zhí)行。


參考錯(cuò)誤:


 // Declare the myGlobal variable below this line

var myGlobal = 10 


function fun1() {

  // Assign 5 to oopsGlobal Here

  oopsGlobal = 5

}


// Only change code above this line


function fun2() {

  var output = "";

  if (typeof myGlobal != "undefined") {

    output += "myGlobal: " + myGlobal;

  }

  if (typeof oopsGlobal != "undefined") {

    output += " oopsGlobal: " + oopsGlobal;

  }

  console.log(output);

}


console.log(oopsGlobal) // ReferenceError: oopsGlobal is not defined




沒(méi)有 ReferenceError(注意是之前fun1()調(diào)用的) console.log()

 // Declare the myGlobal variable below this line

var myGlobal = 10 


function fun1() {

  // Assign 5 to oopsGlobal Here

  oopsGlobal = 5

}


// Only change code above this line


function fun2() {

  var output = "";

  if (typeof myGlobal != "undefined") {

    output += "myGlobal: " + myGlobal;

  }

  if (typeof oopsGlobal != "undefined") {

    output += " oopsGlobal: " + oopsGlobal;

  }

  console.log(output);

}


fun1()

console.log(oopsGlobal)


查看完整回答
反對(duì) 回復(fù) 2023-01-06
  • 2 回答
  • 0 關(guān)注
  • 191 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)