<!DOCTYPE html>
<html lang="en"> 
  <head>
    <meta charset="UTF-8">
    <title>Object.definePrototype()</title></head>
  <body>
    <script>// 定义对象
      var options = {
        el: 'app',
        data: {
          name: '小红',
          age: 22
        },
        methods: {
          play: function() {
            console.log('play...');
          },
          work: function() {
            console.log('work...')
          }
        }
      };
      var data = {
        name: '小黑',
        age: 23
      };
      // 为对象定义访问器属性
      Object.keys(options).forEach(function(key) {
        var key = key;
        var val = options[key];
        Object.defineProperty(options, key, {
          get: function() {
            return val;
            // return options[key];
          },
          set: function(newVal) {
            // options[key] = newVal;
            val = newVal;
            console.log(newVal);
          }
        });
      });
      Object.keys(data).forEach(function(key) {
        var key = key;
        var val = data[key];
        Object.defineProperty(data, key, {
          get: function() {
            return data[key];
          },
          set: function(newVal) {
            val = newVal;
            console.log(newVal);
          }
        });
      });
    </script>
  </body>
</html>

挖坑指南:一个悲伤的故事

挖坑指南:一个悲伤的故事

挖坑指南:一个悲伤的故事 

相关文章: