Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: typeof says "function" but it can't be called as a function

I'm really puzzled with Javascript this time:

var x = Array.prototype.concat.call;
typeof x; // function
x(); // Uncaught TypeError: x is not a function

What on earth is going on here?


If it helps, I also noticed:

  • x([1,2],[3,4]) does not work either

  • toString also thinks it's a function:

    Object.prototype.toString.call(x); // "[object Function]"
    
  • This also happens with Array.prototype.concat.apply.

  • When it is forced as an expression it also does not work:

    (0, Array.prototype.concat.call)([1,2],[3,4]); // Same TypeError
    

Tested in Chrome and Node.

like image 1000
Pedro A Avatar asked May 19 '18 01:05

Pedro A


People also ask

Is typeof a function in JavaScript?

Typeof in JavaScript is an operator used for type checking and returns the data type of the operand passed to it. The operand can be any variable, function, or object whose type you want to find out using the typeof operator.

Why typeof function is function in JS?

typeof is a JavaScript keyword that will return the type of a variable when you call it. You can use this to validate function parameters or check if variables are defined. There are other uses as well. The typeof operator is useful because it is an easy way to check the type of a variable in your code.

Is typeof a function?

The TypeOf function is an important tool when dealing with complex code. It allows a programmer to quickly check a variable's data type—or whether it's “undefined” or “null”—without going through the code line by line! Additionally, the TypeOf function can also check whether an operand is an object or not.

How do you check if a function is a function?

Use the vertical line test to determine whether or not a graph represents a function. If a vertical line is moved across the graph and, at any time, touches the graph at only one point, then the graph is a function. If the vertical line touches the graph at more than one point, then the graph is not a function.


1 Answers

The error is misleading. x is a function, but it has lost the referenced function (concat), which throws an error

Running on firefox gives a more descriptive error

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Called_on_incompatible_type

What it's saying is that the call function has nothing its bound to. In the same way that if you take an object like this:

const a = {
  b: 2,
  test() {
    console.log('hi', this.b);
  }
};
const c = a.test;
c();

You will get hi undefined as you've lost the relationship of the function to b.

You can fix this by either doing c.bind(a)() or c.call(a)

The call function behaves similarly. It is going to be the same for every function, and the pseudocode would look something like this:

class Function {
  constructor(functionDefinition) {
    this.functionDefinition = functionDefinition;
  }

  call(newThis, ...args) {
    // take this.functionDefinition, and call it with `this` and `args`
  }
}

Since you are extracting out the call function, it loses the function object it's associated with.

You can fix this by either binding concat to the function, or using call on call :-)

const a = []
const boundFn = a.concat.call.bind(a.concat)
console.log(boundFn([3], [1,2]));

// Or, you can use `call` to pass in the concat function
const callFn = a.concat.call;
console.log(callFn.call(a.concat, [4], [1,2]))
like image 173
AnilRedshift Avatar answered Nov 09 '22 09:11

AnilRedshift