First, the object
Everything is object in JS.
Ii. Types of objects
Number, String, Boolean, Undefined, function, object, and so on
Use typeof to get the type of object
Such as:
Alert (typeof 123); Number
Alert (typeof NaN); Number
Alert (typeof ' Str '); String
Alert (typeof true); Boolean
Alert (typeof undefined); Undefined
Alert (typeof Math.Abs); function
Alert (typeof null); Object
Alert (typeof []); Object
Alert (typeof {}); Object
Third, packaging objects
1. Packaging objects
Number, Boolean, string all have wrapper objects, wrapper objects are created with new. When the object is wrapped, the value is the same, but the type becomes object.
Such as:
var num = new number (123);
var bool = new Boolean (true);
var str = new String (' abc ');
It is best not to use wrapper objects, especially string types.
2. When using number, Boolean, string, new is not used, which is only equivalent to data type conversions.
var num = number (' 123 '); 123, equivalent to parseint () or parsefloat ()
Alert (typeof num); Number
var bool = Boolean (' true '); True
Alert (typeof bool); Boolean
var bool2 = Boolean (' false '); True non-empty string is true
var bool3 = Boolean ("); False
var str = String (123.45); ' 123.45 '
Alert (typeof str); String
Iv. points to be aware of
Do not new Number()
use new Boolean()
, new String()
create packaging objects;
Use parseInt()
or parseFloat()
to convert any type to number
;
String()
A method that converts any type to string
, or calls directly an object toString()
(null and undefined without the method; The number object uses the ToString () method to require special handling: 123.. ToString (); or (123). ToString (););
It is not usually necessary to convert any type to boolean
re-judgment, because it can be written directly if (myVar) {...}
;
typeof
Operators can determine the,,, number
boolean
string
function
and undefined
;
Judge Array
to use Array.isArray(arr)
;
Judging null
please use myVar === null
;
Determine if a global variable exists typeof window.myVar === ‘undefined‘
;
The function internally determines whether a variable exists typeof myVar === ‘undefined‘
.
JS Learning notes-Standard objects