JS復(fù)習(xí)筆記2(變量進(jìn)階)

1.window全局對(duì)象

let和var對(duì)window全局對(duì)象的影響

var web = 1 //聲明的web會(huì)保存在window全局 對(duì)象中
console.log(window.web) //1

let kolento =1
console.log(window.kolento)  //undefined
//window對(duì)象不會(huì)受到let聲明的影響

//計(jì)算并且打印屏幕距離左側(cè)的距離
console.log(window.screenLeft) 

var screenLeft =1000 //聲明一個(gè)變量與window對(duì)象中自帶的重名情況下,打印我們聲明的
console.log(window.screenLeft)  //1000
//window對(duì)象不會(huì)受到let聲明的影響

2.重復(fù)聲明

var let const的區(qū)別

var a = 1
var a = 2
console.log(a)  //2

let b = 1
let b = 2
console.log(b) //報(bào)錯(cuò)提示,不能重復(fù)聲明,且只針對(duì)于同一個(gè)作用域下

const c = 1 
const c = 2
console.log(c)  //報(bào)錯(cuò)提示,不能重復(fù)聲明,且只針對(duì)于同一個(gè)作用域下

3.變量凍結(jié)

使得 引用類型的變量 也無法被修改

const host={
  url:'www',port:'11'
}
host.port=1
console.log(host.poort) //1

如果不想讓host變量里的內(nèi)容被修改,
Object.freeze(host) //凍結(jié)變量,則無法被js修改

4.傳值與傳址

傳值(字符串 數(shù)值):在數(shù)據(jù)量較小的情況下傳值,等于是在內(nèi)存中新開辟一塊地方復(fù)制 一份數(shù)據(jù),針對(duì)于字符串,數(shù)字等。

傳址(對(duì)象 數(shù)組):針對(duì)于數(shù)據(jù)量較大的情況,幾個(gè)變量共用一塊數(shù)據(jù),由于數(shù)據(jù)較大,所以不反復(fù)復(fù)制大量占用內(nèi)存控件,只傳遞內(nèi)存地址,當(dāng)數(shù)據(jù)發(fā)生改變時(shí),全都改變。

基本類型:字符串 數(shù)值
引用類型:數(shù)組 對(duì)象

//傳值
let a = 1;
let b = a;
console.log(a,b) //1,2
b = 3
console.log(a,b) //1,3

//傳址
let c = {name:'kolento'};
let d= c
console.log(c,d) //{name:'kolento'},{name:'kolento'}
d.name='others'
console.log(c,d) //{name:'others'},{name:'others'}

5.null和 undefined

null:引用類型初始值
undefined:基本類型初始值

let a = 1;
let b ;
console.log(typeof a)  //number
console.log(typeof b)  //undefined
console.log(typeof c)  //undefined,因?yàn)椴淮嬖赾也不存在c的值

6.嚴(yán)格模式

聲明方式:"use strict"
在代碼污染,使用了關(guān)鍵字,不寫var全局聲明等情況下會(huì)報(bào)錯(cuò)。

"use strict"
function  show(){
  a = 1
}
show() //報(bào)錯(cuò) 
//如果把 "use strict"放在函數(shù)內(nèi)則嚴(yán)格模式只對(duì)該函數(shù)起作用
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容