splat over JavaScript object (with new)?

烂漫一生 提交于 2019-12-01 20:42:26

问题


How do I splat across objects without using ECMA6 features?

Attempt

function can(arg0, arg1) {
    return arg0 + arg1;
}

function foo(bar, haz) {
    this.bar = bar;
    this.haz = haz;
}

myArgs = [1,2];

With can I can just do:

can.apply(this, myArgs);

When trying with foo:

new foo.apply(this, myArgs);

I get this error (because I'm calling new):

TypeError: function apply() { [native code] } is not a constructor

回答1:


Using Object.create

function foo(bar, haz) {
    this.bar = bar;
    this.haz = haz;
}

x = Object.create(foo.prototype);
myArgs = [5,6];
foo.apply(x, myArgs);

console.log(x.bar);



回答2:


Using Object.create(proto) is the right way to go about this.

Coco and LiveScript (Coffeescript subsets) offer a workaround:

new foo ...args

compiles to

(function(func, args, ctor) {
  ctor.prototype = func.prototype;
  var child = new ctor, result = func.apply(child, args), t;
  return (t = typeof result)  == "object" || t == "function" ? result || child : child;
  })
(foo, args, function(){});

And in CoffeeScript:

(function(func, args, ctor) {
  ctor.prototype = func.prototype;
  var child = new ctor, result = func.apply(child, args);
  return Object(result) === result ? result : child;
})(foo, args, function(){});

These hacks are ugly, slow, and imperfect; for example, Date relies on its internal [[PrimitiveValue]]. See here.



来源:https://stackoverflow.com/questions/28132226/splat-over-javascript-object-with-new

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!