简体   繁体   中英

instanceof contradicts the object type returned by typeof method

here i have an array of number containing six elements.

var arr=[1.22,2.33,2.002,3.992,3.05,5.44];

then i store each element inside a variable and tried to find out what type of object it is.i tried both typeof and instanceof .typeof saying that the variable i used to contain the array value is a number but instance of saying that it is not a number.so which one is correct..

then i changed the array a little .used string values instead of number inside the array..

var arr=["1.22","2.33","2.002","3.992","3.05","5.44"];

here also typeof declaring the values are number type while instanceof saying the opposite. so can i say it is safe to use typeof instead of instanceof .so my question is when to use typeof and when to use instanceof and how can i avoid this type of confusion??

code:

<script>
    function init() {
        var arr = ["1.22", "2.33", "2.002", "3.992", "3.05", "5.44"];
        for (i = 0; i < arr.length; i++) {
            var bag = arr[i];
            alert(typeof bag == 'string');
            alert(bag instanceof String);
        }
    }
    window.onload = init;
</script>

typeof saying that the variable i used to contain the array value is a number but instance of saying that it is not a number.so which one is correct.

Both. :-)

JavaScript has both primitive and object versions of numbers, strings, and booleans. typeof says "number" for primitive numbers, which are not objects, and so are not instanceof Number .

var a = 5;
console.log(typeof a);            // "number"
console.log(a instanceof Number); // false

It's very rare that you'll ever want to explicitly create and keep a reference to an object version of a number, string, or boolean.

So why do we have them? So we can seem to have methods on primitives. Let's look at my a variable above again, and add another statement to the end:

var a = 5;
console.log(typeof a);            // "number"
console.log(a instanceof Number); // false
console.log(a.toFixed(3);         // "5.000"

Huh? a refers to a primitive number. Primitives don't have methods. So how could I call the toFixed method?

The answer is that the JavaScript engine temporarily promotes the primitive to the equivalent object type, uses that object type's methods, and throws away the object. (In theory; in practice, of course, engines optimize the process.)

You can actually create a Number instance:

var a = new Number(5);
console.log(typeof a);            // "object"
console.log(a instanceof Number); // true

...but again, it's a very, very rare thing to need to do.

instanceof() better to use when you want to check if that variable is instance of someClass .

And typeof() better to use when you want get type of variable, not checks from what class this instance was created.

Also, read how instanceof works here and how works typeof here .

UPD : My lovely example for difference between those:

var array = [];
typeof array == 'array'; // false
typeof array == 'object'; // true
array instanceof Object; // true
array instanceof Array; // true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM