* #type. js
Copy Code code as follows:
function person (name, age) {
THIS.name = name;
This.age = age;
}
var d = {an: ' object '};
var a = [' Apple ', ' banana '];
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.
Copy Code code 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]}
Visible, there is a difference between using the TypeOf operator and the Constucor property to detect object type return values.
If the variable is an array, the TypeOf operator returns object, and the constructor property returns array;
If the variable is a constructor object, the typeof operator returns object, and the constructor property returns the constructor
Each variable has its Construcor attribute, which provides not only this object, but also what type of object this object is. In summary, the constructor property holds a constructor that points to an object, whether it is a custom or a native type object.
It is important to note that different browsers detect regular expressions for typeof operators differently, IE and Firefox return ' object '.
Well, today's content on the first here, the small partners if there is doubt, just leave a message.