- 初始化一个可伸缩长度的数组:
let len = 5
let arr = Array(len).fill(null).map((v, i) => String.fromCharCode(65 + i));
let arr = Array.apply(null, {length: len}).map((v, i) => String.fromCharCode(65 + i));
console.log(arr);
- 对象属性值监听
let data = {
title: 'hello world!'
};
function definePropertyData(obj, prop, value) {
Object.defineProperty(obj, prop, {
get: function () {
console.log(111)
return value;
},
set: function (newValue) {
value = newValue;
},
});
}
definePropertyData(data, 'title', data.title);
console.log(data.title)
data.title = 38;
console.log(data.title)