javascript - Function.prototype.apply doesn't work on Function.prototype.call in Chrome - Stack Overflow

The title should be self explanatory, and I wonder if there is a workaround for this.Window.call.apply;

The title should be self explanatory, and I wonder if there is a workaround for this.

Window.call.apply;
>> function apply()

Window.call.apply();

>> Uncaught TypeError: Window.call.apply is not a function
    at <anonymous>:2:13
    at Object.InjectedScript._evaluateOn (<anonymous>:895:140)
    at Object.InjectedScript._evaluateAndWrap (<anonymous>:828:34)
    at Object.InjectedScript.evaluate (<anonymous>:694:21)

Basically, I am surprised that apply works on every function except one.

I am writing some higher level VM that needs to call native javascript functions (sadly, Function.prototype.call is one of them), so I need the function apply to be able to pass my this context and array of arguments.

Updated: It seems that this is just some weird error message. I found this when I wanted to be able to call a constructor with variable number of arguments and passing this explicitly, which is an object instead of the expected function.

Perhaps below is a more realistic example:

function Fruit(name) {
  this.name = name;
}
var x = {};
Fruit.call(x, "Apple");
// I thought this would also work, but it throws above error message
(Fruit.call).apply(x, ["Apple"]);
// Should instead use the constructor directly
Fruit.apply(x, ["Apple"]);
// or
(Fruit.call).apply(someFunction, [x, "Apple"]);

The title should be self explanatory, and I wonder if there is a workaround for this.

Window.call.apply;
>> function apply()

Window.call.apply();

>> Uncaught TypeError: Window.call.apply is not a function
    at <anonymous>:2:13
    at Object.InjectedScript._evaluateOn (<anonymous>:895:140)
    at Object.InjectedScript._evaluateAndWrap (<anonymous>:828:34)
    at Object.InjectedScript.evaluate (<anonymous>:694:21)

Basically, I am surprised that apply works on every function except one.

I am writing some higher level VM that needs to call native javascript functions (sadly, Function.prototype.call is one of them), so I need the function apply to be able to pass my this context and array of arguments.

Updated: It seems that this is just some weird error message. I found this when I wanted to be able to call a constructor with variable number of arguments and passing this explicitly, which is an object instead of the expected function.

Perhaps below is a more realistic example:

function Fruit(name) {
  this.name = name;
}
var x = {};
Fruit.call(x, "Apple");
// I thought this would also work, but it throws above error message
(Fruit.call).apply(x, ["Apple"]);
// Should instead use the constructor directly
Fruit.apply(x, ["Apple"]);
// or
(Fruit.call).apply(someFunction, [x, "Apple"]);
Share Improve this question edited Jun 22, 2015 at 15:22 Evan Sebastian asked Jun 22, 2015 at 14:44 Evan SebastianEvan Sebastian 1,74414 silver badges20 bronze badges 4
  • In Firefox I get TypeError: Function.prototype.call called on inpatible undefined, which makes sense. – Felix Kling Commented Jun 22, 2015 at 14:46
  • Huh, I guess only Chrome has this issue then. – Evan Sebastian Commented Jun 22, 2015 at 14:47
  • Nah, the error message is just different and confusing. Window.call.apply(function foo() { console.log('foo'); }); works perfectly fine. – Felix Kling Commented Jun 22, 2015 at 14:49
  • Apply call on Window and it'll work? – Bergi Commented Jun 22, 2015 at 14:50
Add a ment  | 

1 Answer 1

Reset to default 3

You are getting the error because you are not passing an argument to .apply. Firefox throws a more reasonable error:

TypeError: Function.prototype.call called on inpatible undefined

It works fine in both browsers if you actually pass a function that should be called:

Window.call.apply(function foo() { console.log('foo'); });


Note: If you actually want to apply the Window function to an arbitrary object, this probably won't work. Host objects/functions are often more restrictive, especially when it involves the DOM.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745313043a4622088.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信