Home  >  Article  >  Web Front-end  >  typeof and type judgment in JS (with code)

typeof and type judgment in JS (with code)

php中世界最好的语言
php中世界最好的语言Original
2018-04-16 14:32:441509browse

This time I will bring you typeof and type judgment in JS (with code). What are the precautions for typeof and type judgment in JS? The following is a practical case. Let’s take a look. .

typeof

ECMAScript has 5 primitive types, namely Undefined, Null, Boolean, Number and String. We all know that you can use the typeof

operator to find the type of a variable, but when is used to reference a type variable, it will only return object, which means that typeof can only Correctly identify basic type value variables.

var a = "abc";
console.log(typeof a); // "string"
var b = 123;
console.log(typeof b); // "number"
var c = true;
console.log(typeof c); // "boolean"
var d = null;
console.log(typeof d); // "object"
var f = undefined;
console.log(typeof f); // "undefined"
var g;
console.log(typeof g); // "undefined"
console.log(typeof x); // "undefined"
You may ask why the typeof operator returns "object" for null values. This was actually a bug in

JavaScript's original implementation, which was then adopted by ECMAScript. Now, null is considered a placeholder for the object, thus explaining the contradiction, but technically it is still a primitive value.

The last one is strange, typeof a non-existent variable x actually returns "object" instead of "undefined".

We are coming to the following code:

var a = function() { };
console.log(typeof a); // "function"
var b = [1,2,3]; 
console.log(typeof b); // "object"
var c = { };
console.log(typeof c); // "object"
"Object" is returned for both arrays and objects, so a common need in our daily development is how to determine whether a variable is an array or an object.

Type judgment

Type judgment generally means judging whether it is an array or an empty object. This is the judgment method I have used or seen every day for this requirement

Determine whether it is an array

There is an array: var a = [1,2,3,4,5];

method one:

toString.call(a); // "[object Array]" method two:

a instanceof Array; //true method three:

a.constructor == Array; //true The first method is more general, which is the abbreviation of Object.prototype.toString.call(a).

The variables judged by instanceof and constructor must be declared on the current page. For example, a page (parent page) has a frame, and the frame refers to a page (child page). An a is declared in the child page and assigned a value. Give a variable to the parent page, then judge the variable, Array == object.constructor will return false;

var a = [1,2,3,4,5];
console.log(toString.call(a)); // "[object Array]"      
console.log(a instanceof Array); //true
console.log(a.constructor == Array); //true
Determine whether it is an empty object

There are variables: var obj = {};

method one:

JSON.stringify(obj); // "{}" is converted into a JSON object to determine whether it is an empty brace

Method Two:

if(obj.id){ //If

attributeid exists....} This method is relatively simple, and most people can think of it, provided that there is a certain attribute in the object.

Method three:

function isEmptyObject(e) { 
var t; for (t in e) return !1; return !0 } //trueisEmptyObject(obj);
//falseisEmptyObject({ "a":1, "b":2});
This method is the implementation of jQuery's isEmptyObject() method.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:



The above is the detailed content of typeof and type judgment in JS (with code). For more information, please follow other related articles on 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