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

Detailed explanation of JavaScript implementation of data types and array judgment methods

韦小宝
Release: 2018-03-14 18:39:03
Original
1275 people have browsed it

This article tells about JavaScriptHow to implement Data types and the judgment of arrays. If you don’t understand the judgment of JavaScript’s implementation of data types and arrays, or you don’t know about JavaScript’s implementation of data types and arrays If you are interested in the judgment, let’s take a look at this article. Okay, let’s stop talking and get to the point!

Written at the beginning:
During yesterday’s interview, I found that I didn’t answer a very, very simple question. Maybe it was because I was too nervous and felt like I was being made to cry. Later, I thought about it, I should record it carefully so that I can have a deep impression. The revolution has not yet succeeded, and we still need to work hard to become strong!

1. The six major data types of js

number:数字,整数、浮点数等等,
string:单引号或者双引号来说明,
Boolean:返回true和false,这两个值不一定对应1和0
object:对象,可以执行new操作符后跟要创建的对象类型的名称来创建。
null:只有一个值得数据类型,逻辑上讲,null值表示一个空对象指针。
undefined:未定义,使用var声明变量但未对其初始化时,变量的值就是undefined。
Copy after login

2. Data type judgment typeof
typeof can solve the judgment of most data types, and its return value is a string, which describes the type of the operand.

//判断变量num是不是一个数字类型
if(typeof num=='number') {
    return true;
}
Copy after login

Return results:

var a="hling"; console.log(a); //string
var a=1; console.log(a); //number
var a=false; console.log(a); //boolean
var a; console.log(typeof a); //undfined

var a = null; console.log(typeof a); //object
var a = document; console.log(typeof a); //object
var a = []; console.log(a); //object

var a = function(){}; console.log(typeof a) 
//function除了可以判断数据类型还可以判断function类型
Copy after login

In addition to the four types of string, number, boolean, and undefined, null, object, and array all return object types! ! !

For function types, function is returned, such as typeof(Date), typeof(eval), etc.

3. How to determine array type in js

1) instanceof
Instanceof is used to determine whether a variable is an instance of an object. It is a ternary operator. This operator has something to do with Object-oriented in JavaScript. To understand this, you must first understand object-oriented in JavaScript. Because this operator detects whether the prototype chain of the object points to the prototype object of the constructor .

a instanceof b?alert("true"):alert("false")  
//注意b值是你想要判断的那种数据类型,是不是一个字符串,比如Array
Copy after login
例子:var arr = [1,2,3,1]; 
alert(arr instanceof Array); // true
Copy after login

2) constructor
Definition in the W3C definition: The constructor attribute returns a reference to the array function that created this object

var arr = []; 
arr instanceof Array; // true 
arr.constructor == Array; //true
Copy after login

Judges various types The method is:

console.log("string".constructor == String);
console.log((123).constructor == Number);
console.log(false.constructor == Boolean);
console.log([].constructor == Array);
console.log({}.constructor == Object);
Copy after login

General method:

function isArray(object){
    return object && typeof object==='object' &&
            Array == object.constructor;
}
Copy after login

3) Characteristic judgment
object.isArray() to judge, the purpose is to accurately detect whether a value is an array. IE9+, Firefox 4+, Safari 5+, Opera 10.5+ and Chrome all implement this method. However, versions before IE8 are not supported.

function isArray(object){
    return  object && typeof object==='object' &&    
            typeof object.length==='number' &&  
            typeof object.splice==='function' &&    
             //判断length属性是否是可枚举的 对于数组 将得到false  
            !(object.propertyIsEnumerable('length'));
}
Copy after login

4) Object.prototype.toString.call

Object.prototype.toString.call(value) == '[object Array]'
Copy after login

The above is all the content of this article. If you don’t know much about it, you can implement it yourself. It’s easy to master both sides!
Related recommendations:
js determines whether it is ie browser

JS Detailed introduction to judgment array

The above is the detailed content of Detailed explanation of JavaScript implementation of data types and array judgment methods. For more information, please follow other related articles on 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