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

Js determines whether the parameter (String, Array, Object) is undefined or the value is empty_javascript skills

WBOY
Release: 2016-05-16 17:17:40
Original
1635 people have browsed it

Sometimes we encounter such a situation: During the data verification process when some front-end controls submit data to the server, it is necessary to determine whether the submitted data is empty. If it is string data in an ordinary form, you only need to determine the length after trim. The data required here can be of various types (numbers, strings, arrays, objects, etc.), through JSON.stringify(data ) is serialized and then passed.

The following data values ​​are defined here as "null values":

•undefined
•null
•Empty string and pure blank string: '', ' ' etc.
•Empty array: []
•Empty object: {}

For data values ​​other than this, they are considered not to be empty.

Among them, null and undefined are easy to identify, but for other types, we need to get the data type before we can use the corresponding method to detect whether the data is empty. The easiest way to think of is to use the typeof operator:

Copy the code The code is as follows:

< ;SPAN style="BACKGROUND-COLOR: rgb(255,255,255)">if(typeof data === 'number') {
//deal with numbers
}

But typeof returns only six types of strings: 'object', 'function', 'number', 'boolean', 'string', and 'undefined'. Many native objects such as Date and RegExp objects cannot be used. {} to distinguish objects created. In addition, typeof will return different values ​​for some basic data types such as (String, Number, Boolean) and their corresponding basic packaging type data, such as:
Copy code The code is as follows:

console.log(typeof false); //'boolean'
console.log(typeof new Boolean(false)); //'object'
console.log(typeof 1); //'number'
console.log(typeof new Number(1)); //'object'
console.log(typeof ''); //'string'
console.log(typeof new String('')); //'object'


This also has a certain impact on our judgment.

Use instanceof? This can only judge objects, and there is a problem that multiple objects of the same type do not share prototypes when there are multiple frames. Objects obtained from other frames cannot be correctly judged.

Fortunately, there is another simplest and most reliable method: Object.prototype.toString. For different types of data, this method can return strings such as '[object Object]', '[object Array]', and '[object String]', which is very convenient for judgment. It should be noted that in IE8 and below browsers, this method will return '[object Object]' for null, undefined, window, etc., but fortunately, this does not affect our use of it to determine empty objects.

The code is directly below, and the explanation can be found in the comments.

Copy code The code is as follows:

var isEmptyValue = function(value) {

var type;
if(value == null) { // Equivalent to value === undefined || value === null
return true;
}
type = Object. prototype.toString.call(value).slice(8, -1);
switch(type) {
case 'String':
return !$.trim(value);
case ' Array':
                   return !value.length; >                                                                                                                                                                                                                 ;

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