When, if at all, Javascript "typeof" returns the wrong type?

I am trying to make my way through the code of another, and I find some bizarre things. I found the following code in the program part:

if(typeof func == "function" || (typeof func == "object" && typeof func.document == "undefined"))
{
    func();
}

It made no sense to me, because I was not sure when such an event could happen? Can typeof return the wrong type? Or is it possible that an object that is not a function might be called?

The code is a mess, so I cannot find an example of where this particular check can be successful. I should note that the code may be older than 10 years. The statement I received was that it was required because sometimes, when the code was originally written, typeof did not return the function as a type.

In addition, this check was required because sometimes func was passed to the window (?) Object, so you had to make sure that it was not a window object either.

+4
source share
2 answers

Remember: typeoffor literals.

typeof undefined === "undefined"
typeof 5 === "number"
typeof true === "boolean"
typeof "" === "string"
typeof {} === "object"
typeof [] === "object"
typeof function { } === "function"

// null is a weird exception:
typeof null === "object"

instanceofdistinguishes when something has “functions” (note that where typeof checks the string, "object"instanceof checks directly for the functions Object:

{} instanceof Object

function {} instanceof Object
function {} instanceof Function

[] instanceof Object
[] instanceof Array

As you can see, typeof says === "function"and !== "object": this can be misleading because the function is also an object. This is when instanceof comes into play.

Now, when you write a constructor, the constructed object is also an instance of your function:

// the constructor
function Example { }

// the object
var ex = new Example();

typeof Example === "function"
typeof ex === "object"
ex instanceof Object
ex instanceof Example

, , typeof :

function DerivedExample { }

DerivedExample.prototype = new Example();
// DerivedExample.prototype.constructor = DerivedExample;

var ex1 = new DerivedExample();

typeof DerivedExample === "function"
typeof ex1 === "object"
ex1 instanceof Object
ex1 instanceof Example
ex1 instanceof DerivedExample

, javaScript typeof, instanceof. , , .


- ( , CoffeeScript):

typeof new Number(5) === "object"
new Number(5) instanceof Object
new Number(5) instanceof Number

typeof new Boolean("true") === "object"
new Boolean("true") instanceof Object
new Boolean("true") instanceof Boolean

typeof new String("") === "object"
new String("") instanceof Object
new String("") instanceof String

window:

.
( ), "use strict" "", undefined.

function world() {
    // non-strict: this === window
    // use-strict: this === undefined
}
world();

, , :

var hello = {
    world: function () {
        // this === hello
    }
};
hello.world();

, :

function Hello() { }

Hello.prototype.world = function () {
    // this instanceof Hello
    // this will be === ex
};

var ex = new Hello();
ex.world();

, call apply:

var ex = { };

function Example() {
    // (1) non-strict: this === window
    // (1) use-strict: this === undefined
    // (2) this === ex
}

Example(); // (1)
Example.call(ex);  // (2)
Example.apply(ex); // (2)
+5

var a = [];
typeof a; // object
Object.prototype.toString.call(a); // [object Array]

var b = {};
typeof b; // object
Object.prototype.toString.call(b); // [object Object]

, , typeof

0

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


All Articles