1. Data type
JavaScript has a total of six data types. (ES6 added a value for the seventh symbol type)
- Value (number)
- Strings (String)
- Boolean Value (Boolean)
- Undefined
- Null
- Objects (object)
2. Data type determination
JavaScript has three ways to determine the type of a value
- typeof operator
- instanceof operator
- Object.prototype.toString () method
typeof operator
typeof
The operator can return the data type of a value.
Values, strings, and Boolean values are returned number
, respectively string
boolean
.
typeof 123 //"number"typeof ‘hello‘ //"string"typeof true //"boolean"
function to return function
.
function f(){} typeof f //"function"
undefined
Return undefined
.
typeof undefined // "undefined"
object is returned object
.
typeof {} // "object"typeof [] // "object"
Null 返回
object '.
typeof null // "object"
instanceof operator
instanceof
The operator returns a Boolean value that indicates whether the object is an instance of a constructor.
Because the instanceof
entire prototype chain is checked, the same instance object may be returned to more than one constructor true
.
instanceof
One use of the operator is to determine the type of the value.
var x = []var f={}x instanceof Array //truef instanceof Object //true
instanceof
The operator can only be used with objects, not values of the original type.
With the instanceof
operator, you can also resolve the problem of adding a command when calling new
the constructor.
function Fn (f1, f2) { if (this instanceof Fn) { this._foo = f1; this._bar = b2; } else { return new Fn(f1, f2); }}
Object.prototype.toString ()
toString
The function of a method is to return a string form of an object, returning a type string by default.
var o1 = new Object();o1.toString() //"[object Object]"
Application of ToString (): Judging data type
Object.prototype.toString
method returns the type string of an object, so it can be used to determine the type of a value.
var obj = {};obj.toString() // "[object Object]"
The above code calls a method of an empty object, and the toString
result returns a string object Object
with the second Object
constructor representing the value. This is a very useful method of judging the data type.
Because instance objects may have custom toString
methods that override methods, Object.prototype.toString
it is best to use the method directly in order to get the type string Object.prototype.toString
. By means of a function call
, you can call this method on any value to determine the type of the value.
Object.prototype.toString.call(value)
The above code indicates that the value
method is called on this value Object.prototype.toString
.
The method return values for different data types Object.prototype.toString
are as follows.
- Value: Returns
[object Number]
.
Object.prototype.toString.call(12) //"[object Number]"
- String: Returns
[object String]
.
Object.prototype.toString.call(‘ab‘) //"[object String]"
- Boolean value: Returns
[object Boolean]
.
Object.prototype.toString.call(true) //"[object Boolean]"
- Undefined: return
[object Undefined]
.
Object.prototype.toString.call(undefined) //"[object Undefined]"
- Null: Returns
[object Null]
.
Object.prototype.toString.call(null) //"[object Null]"
- Array: Returns
[object Array]
.
Object.prototype.toString.call([]) //"[object Array]"
- Function: Returns
[object Function]
.
var f = function (){}Object.prototype.toString.call(f) //"[object Function]"
Using this feature, you can write a typeof
type-judging function that is more accurate than the operator.
var type = function (o){ var s = Object.prototype.toString.call(o); return s.match(/\[object (.*?)\]/)[1].toLowerCase();};type({}); // "object"type([]); // "array"type(3); // "number"type(null); // "null"type(); // "undefined"type(/abcd/); // "regex"type(new Date()); // "date"
Not to be continued
Data type of 01.javascript