typeof
When typeof is used more often, it is to determine whether a global variable is present, if a page defines a global variable. If you make the following judgment:
//harooms is a global variable if(harooms!=undefined){}//js will report an error saying "Uncaught ReferenceError: harooms is not defined"The solution is to write as follows:
if(typeof haorooms!=undefined){ }After using typeof, there will be no error! This is one of the typesof applications!
In addition, typeof can also make judgments on data types! as follows:
var harooms="string"; console.log(harooms); //stringvar harooms=1; console.log(harooms); //numbervar harooms=false; console.log(harooms); //booleanvar harooms; console.log(typeof harooms); //undfinedvar harooms= null; console.log(typeof harooms); //objectvar harooms = document; console.log(typeof harooms); //objectvar harooms = []; console.log(harooms); //objectvar harooms = function(){}; console.log(typeof harooms) //function In addition to judging data types, you can also judge function typesObviously, for typeof, except for the first four types, null, object, and array return all object types;
instanceof
It can be used to determine whether it is an array.
var harooms=[];console.log(harooms instanceof Array) //Return true
constructor
The constructor is the constructor that returns the corresponding constructor of the object.
How to judge various data types:
console.log([].constructor == Array);console.log({}.constructor == Object);console.log("string".constructor == String);console.log((123).constructor == Number);console.log(true.constructor == Boolean);function employee(name,job,born){ this.name=name; this.job=job; this.born=born; }var haorooms=new employee("Bill Gates","Engineer",1985); console.log(harooms.constructor); //Output function employee(name, jobtitle, born){this.name = name; this.jobtitle = job; this.born = born;}By outputting haorooms.constructor, it can be seen that constructor returns the constructor corresponding to the object.
Object.prototype.toString
We mentioned earlier that we can use the constructor attribute to determine the object type. Let's talk about the Object.protype.toString method.
Object.prototype.toString.apply({}) // "[object Object]"Object.prototype.toString.apply([]) // "[object Array]"Object.prototype.toString.apply(NaN)// "[object Number]"Object.prototype.toString.apply(function(){}) // "[object Function]"Using this method, we can correctly judge the basic pattern of a variable, but if it is a custom type, we cannot know the real type, because the result will still be [object Object]
other
jQuery also has a type judgment method, the following is an example
$.isWindow(window) // true
How to do it
core.js#479isWindow: function( obj ) { return obj != null && obj == obj.window;}So open an Object like this:
var fakeWindow;fakeWindow = {};fakeWindow.window = fakeWindow;$.isWindow(fakeWindow) // trueYou lied to him.
summary
In JavaScript, you must correctly judge types. When you study them carefully, it is really a troublesome. It is very important to design your judgment according to different situations. We must also think about how to judge the correct type in the simplest way. Of course, there are still many things that have not been introduced in this article. For example, isPrototypeOf method. JavaScript is a language with many historical burdens, but it is also constantly improving. When using it, you should pay attention to too many ways of double-sided blades. Remember to use them carefully.