1. Initialize an array of scalable length:
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); //[ 'A', 'B', 'C', 'D', 'E' ]
Copy the code
  1. Object property value listens
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) // 111 hello world!
data.title = 38;
console.log(data.title) / / 111 38
Copy the code