Object.defineProperty(window, PI, {
configurable: false,
writeable: false,
value: 3.1415
});
console.log(PI); // 3.1415
/* if 'use strict', the acts below would throw error */
PI = 0; // nothing happened
window.PI = 0; // nothing happened
delete window.PI; // nothing happened
Object.defineProperty(window, PI, {
configurable: true,
writeable: true,
value: 0
}); // nothing happened
console.log(PI); // 3.1415
配列またはオブジェクトは、その属性を「const」として定義することもできます
Object.defineProperties
しかし、最速の方法は
Object.freeze(obj_name)
ただし、オブジェクト自体が「const」であることを確認する必要があり、その属性は実際に安全です