admin管理员组

文章数量:1419640

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.

本文标签: javascriptFunctionprototypeapply doesn39t work on Functionprototypecall in ChromeStack Overflow