TypeError:代理上的'ownKeys':陷阱结果不包含'参数'

时间:2016-10-01 20:12:47

标签: javascript node.js v8 proxy-classes

它对我有什么要求?如何使它工作?

var proxy_handler = 
{
    ownKeys: function(target)
    {
        return Object.keys(target.data)
    },
}

var proxxxy = function(initial_data)
{
    var return_value = "Goodbye world"
    var target = function() { return return_value }
    if(typeof initial_data == "undefined")
    {
        target.data = {}
    }
    else
    {
        target.data = initial_data
    }
    return new Proxy(target, proxy_handler)
}

var p = proxxxy({q:"aaa",w:"bbb",f:"ccc"})
console.log(p())
console.log(Object.getOwnPropertyNames(p))

它会输出错误但不应该:

me@me:~/tst$ node --version
v6.2.2
me@me:~/tst$ node test3.js 
Goodbye world
/home/me/tst/test3.js:26
    console.log(Object.getOwnPropertyNames(p))
                       ^

TypeError: 'ownKeys' on proxy: trap result did not include 'arguments'
    at Object.<anonymous> (/home/me/tst/test3.js:26:24)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.runMain (module.js:575:10)
    at run (node.js:348:7)
    at startup (node.js:140:9)
    at node.js:463:3

这是一个错误吗?如果是这样 - 我可以提交它吗?

1 个答案:

答案 0 :(得分:3)

这不是一个错误;此行为由ownKeys target定义,步骤17a。在简单的英语中,实际ownKeys的任何不可配置属性必须出现在arguments返回的属性列表中,因此示例中缺少> Object.getOwnPropertyDescriptor(target, "arguments") Object {value: null, writable: false, enumerable: false, configurable: false}

(function() {
  var canvas = this.__canvas = new fabric.Canvas('c');
  fabric.Object.prototype.transparentCorners = false;

  var polygon;
  var padding = 0;
  fabric.Image.fromURL('http://fabricjs.com/assets/pug.jpg', function(img) {

    img.scaleToWidth(100);
    
    var patternSourceCanvas = new fabric.StaticCanvas();
    patternSourceCanvas.add(img);

    var pattern = new fabric.Pattern({
      source: function() {
        patternSourceCanvas.setDimensions({
          width: img.getWidth() + padding,
          height: img.getHeight() + padding
        });
        return patternSourceCanvas.getElement();
      },
      repeat: 'repeat'
    });

    polygon = new fabric.Polygon([
      {x: 185, y: 0},
      {x: 250, y: 100},
      {x: 385, y: 170},
      {x: 0, y: 245} ], {
        left: 0,
        top: 70,
        angle: -15,
        fill: pattern,
        objectCaching: false
    });
    canvas.add(polygon);

    controlKeyDown = false;
    document.getElementById('c').focus();
    //setInterval only used in case you go "Full page" in the code snippet demo tool
    setInterval(function() {
      document.getElementById('c').focus();
    }, 1500);
    document.addEventListener('keydown', function(event) {
      var keyCode = ('which' in event) ? event.which : event.keyCode;
      controlKeyDown = (keyCode == 17);
    });
    document.addEventListener('keyup', function(evn) {
      controlKeyDown = false;
    });
    
    var mouseX;
    var mouseY;
    canvas.on('mouse:move', function(evn) {
      mouseX = evn.e.offsetX;
      mouseY = evn.e.offsetY;
    });

    canvas.on('object:selected', function(e) {
      if (controlKeyDown) {
        var obj = e.target;
        //pattern position change
        polygon.points[0].x += mouseX;
        polygon.points[0].y += mouseY;
        polygon.points[1].x += 80;
        polygon.points[1].y -= 80;
        polygon.points[2].x += 60;
        polygon.points[2].y += 60;
        polygon.points[3].x += 80;
        polygon.points[3].y -= 80;
      }
    });
  });
})();