This article mainly introduces information about the typeof operator and constucor attribute detection in javascript. For more information, see * # type. js.
The code is as follows:
Function Person (name, age ){
This. name = name;
This. age = age;
}
Var d = {an: 'object '};
Var a = ['apple', 'bana'];
Var f = function (){};
Var s = 'David ';
Var n = 33;
Var B = true;
Var o = new Object ();
Var person = new Person ('Mark', 22 );
Console. log (typeof (d) + ':' + d. constructor );
Console. log (typeof (a) + ':' + a. constructor );
Console. log (typeof (f) + ':' + f. constructor );
Console. log (typeof (s) + ':' + s. constructor );
Console. log (typeof (n) + ':' + n. constructor );
Console. log (typeof (B) + ':' + B. constructor );
Console. log (typeof (o) + ':' + o. constructor );
Console. log (typeof (person) + ':' + person. constructor );
Run $ node type. js
The code is as follows:
Object: function Object () {[native code]}
Object: function Array () {[native code]}
Function: function Function () {[native code]}
String: function String () {[native code]}
Number: function Number () {[native code]}
Boolean: function Boolean () {[native code]}
Object: function Object () {[native code]}
Object: function Person () {[native code]}
It can be seen that there are differences between using the typeof operator and the constucor attribute to detect the return values of object types.
If the variable is an Array, the typeof operator returns the object, and the constructor attribute returns the Array;
If the variable is a constructor object, the typeof operator returns the object, and the constructor attribute returns the constructor.
Each variable has its construcor attribute. this attribute not only provides the object or not, but also provides the object type. in short, the constructor attribute stores a constructor pointing to an object, whether it is a custom object or a native object.
One thing to note is that different browsers will have different regular expressions for the typeof operator detection, and IE and Firefox will return 'object '.
Well, today's content is here first. if you have any questions, leave a message below.