Strange behavior in the Chrome console

Take a look at the screenshot ...

screenshot

The reason I'm experimenting is because I did a simple google search to check if a parameter is a function, and I found this ...

var getClass = {}.toString; ... function isFunctionA(object) { return object && getClass.call(object) == '[object Function]'; } 

source: http://jsperf.com/alternative-isfunction-implementations/4

So what is the difference between what I print and the source code example? Why does Chrome give an error when entering only {}.toString , but it works fine when it is inside the bracket?

+4
source share
1 answer

{} At the beginning of the instruction is ambiguous, is it an empty block of code or an object? The definition allows ambiguity, defining it as a code block, use {} at the beginning of the statement, since the object makes it an expression, enclosing it in parentheses: ({})

See also the answer to Why do you throw a SyntaxError when accessing an object directly in a literal object?

+4
source

Source: https://habr.com/ru/post/1438350/


All Articles