Home  >  Article  >  Web Front-end  >  Summary of methods to determine data type in JavaScript

Summary of methods to determine data type in JavaScript

高洛峰
高洛峰Original
2017-01-14 10:19:191071browse

typeof
Typeof is often used to determine whether a global variable exists. If a page defines a global variable. If you make the following judgment:

//haorooms是全局变量
if(haorooms!=undefined){
}//js会报错,说"Uncaught ReferenceError: haorooms is not defined"

The solution is for us to write as follows:

if(typeof haorooms!=undefined){
}

After using typeof , no error will be reported! This is one of the applications of typeof!

In addition, typeof can also determine the data type! As follows:

var haorooms="string"; console.log(haorooms); //string
var haorooms=1; console.log(haorooms); //number
var haorooms=false; console.log(haorooms); //boolean
var haorooms; console.log(typeof haorooms); //undfined
 
var haorooms= null; console.log(typeof haorooms); //object
var haorooms = document; console.log(typeof haorooms); //object
var haorooms = []; console.log(haorooms); //object
var haorooms = function(){}; console.log(typeof haorooms) //function  除了可以判断数据类型还可以判断function类型

Obviously, for typeof, in addition to the first four types, null, object, and array return all object types;

instanceof
You can use it to determine whether it is an array.

var haorooms=[];
console.log(haorooms instanceof Array) //返回true

constructor
constructor is the constructor corresponding to the returned object.
Methods to determine 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(haorooms.constructor); //输出function employee(name, jobtitle, born){this.name = name; this.jobtitle = job; this.born = born;}

By outputting haorooms.constructor, you can see that constructor is the constructor corresponding to the returned object.

Object.prototype.toString
We mentioned earlier that we can use the constructor attribute to determine the object type. Let us talk about the Object.protype.toString method again

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 determine the basic type of a variable, but if it is a custom type, we cannot know the real type, because the result will still be [object Object]

Others
jQuery also has a method of type judgment. Here is an example

$.isWindow(window) // true

How to do it

core.js#479
isWindow: function( obj ) {
  return obj != null && obj == obj.window;
}

So open an Object like this:

var fakeWindow;
fakeWindow = {};
fakeWindow.window = fakeWindow;
$.isWindow(fakeWindow) // true

You just lied to him.

Summary
It is really troublesome to correctly judge the type in JavaScript. When you study it carefully, it is very important to design your judgment expression according to different situations. We must also Think about how to determine the correct type in the most concise way. Of course, there are many places that are not introduced in this article, such as the isPrototypeOf method. JavaScript is a language with a lot of historical baggage, but it is also constantly improving. When using it , please note that there are too many methods that are double-edged, so remember to use them with caution.

For more related articles on methods to determine data types in JavaScript, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn