在ES5中,一般利用window對(duì)象來定義常量,思路是將變量綁在window上,并設(shè)為只讀狀態(tài)。
代碼:
Object.defineProperty(window,"PI",{
value:3.14,
writeable:false,//只讀
});
console.log(window.PI);
ES6中,則可以直接通過const來聲明常量。
const PI = 3.14;
console.log(PI);
在ES5中,一般利用window對(duì)象來定義常量,思路是將變量綁在window上,并設(shè)為只讀狀態(tài)。
代碼:
Object.defineProperty(window,"PI",{
value:3.14,
writeable:false,//只讀
});
console.log(window.PI);
ES6中,則可以直接通過const來聲明常量。
const PI = 3.14;
console.log(PI);