Pat*_*at8 -2 javascript readonly-attribute
我对只读属性的含义有些困惑?我知道这classList是 MDN 定义的只读属性,但这究竟是什么意思?
只读属性意味着它不能被覆盖或分配。在非严格模式下,任何此类分配都将无声无息地执行任何操作。例如:
var obj = {};
Object.defineProperty(obj, 'property', {value: 123, writeable: false})
// Assign 456 to obj.property using the . syntax, but it's still 123
obj.property = 456;
console.log(obj.property);
// Assign 789 to obj.property using the [] syntax, but it's still 123
obj['property'] = 789;
console.log(obj['property']);Run Code Online (Sandbox Code Playgroud)
或者只是错误地使用TypeError严格的 mide:
'use strict';
var obj = {};
Object.defineProperty(obj, 'property', {value: 123, writeable: false})
// Assign 456 to obj.property in strict mode will result in a TypeError
obj.property = 456;
console.log(obj.property);Run Code Online (Sandbox Code Playgroud)