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ù)起作用