Home > Web Front-end > JS Tutorial > body text

Summary of methods to determine data type in JavaScript

高洛峰
Release: 2017-01-14 10:19:19
Original
1071 people have browsed it

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"
Copy after login

The solution is for us to write as follows:

if(typeof haorooms!=undefined){
}
Copy after login

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类型
Copy after login

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
Copy after login

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;}
Copy after login

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]"
Copy after login

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
Copy after login

How to do it

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

So open an Object like this:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!