First, array detection
1. Use of Array.isarray ()
For example:
Array.isarray ([])//true
array.isarray ({})//false
Compatibility:
Chrome |
FIREFOX |
Ie |
OPERA |
Safari
|
5 |
4.0 (2.0) |
9 |
10.5 |
5 |
You can use the following methods to detect whether support is supported first Array.isArray
.
if (Array.isarray) {return
array.isarray (obj);
}
2. Use of instanceof
For example:
var arr=[];
Console.log (arr instanceof Array); True
3. Using the OBJECT.PROTOTYPE.TOSTRING method
if (Object.prototype.toString.call (arr) = = ' [Object Array] ') {
console.log ("yes");
}
4. Using the constructor method
function IsArray (obj) {return
!! obj && Array = = Obj.constructor;
}
Second, type detection
The typeof operator detects the data type of a given variable
typeof operand//operand is an expression that represents an object or original value
Here are some common types of return results, and it is worth noting that null returns object, and that the reference type is actually judged as object.
type |
result |
|
|
null |
boolean |
" Boolean " |
number |
"number" |
string |
"string" |
symbol (New in ECMAScript 2015) |
|
host object (provided by the JS environment) |
implementation-dependent |
function object (implements [[[Call]] in ECMA-262 terms) |
|
any other object |
"object" |
Third, object type detection
1. instanceof
Used to detect the constructor.prototype
existence of an object on a prototype chain.
How to use:
Object Instanceof Constructor
As an example:
function A () {};
var a=new a ();
A instanceof a//true
a instanceof object//true
a.prototype instanceof Object//true
2. isprototypeof
Returns true as long as the prototype of an instance derived from the prototype chain
Object.prototype.isPrototypeOf (instance);
Notice here that the difference with the instanceof, in the constructors is replicated, and without the redesign of the case, you can use the Isprototype
var a = {
//something
}
var B = Object.create (a);
var C = object.create (B);
Console.log (a.isprototypeof (C)); True
console.log (C instanceof A); TypeError
3. hasOwnProperty
hasOwnProperty()
method is used to determine whether an object contains a specified own property.
4. Object.is ()
Used to compare two values for strict equality, same as = =
Iv. Basic types of testing
Number
Number.isFinite()
Used to check whether a number is finite (finite)
Number.isNaN()
Used to check whether a value is Nan
Number.isInteger()
Used to determine whether a value is an integer. In JavaScript, integers and floating-point numbers are the same storage method, so 1 and 1.0 are the same value.
Number.isSafeInteger()
JavaScript can accurately represent an integer range between -2^53 and 2^53 (excluding two endpoints), exceeding this range and cannot accurately represent this value.
Summarize
Some of the methods currently in contact will be updated if there is a new future. Interested friends can continue to pay attention to the cloud habitat community, well, the above is the entire content of this article, I hope that the study or work can bring some help.