*#
function Person(name, age) {
= name;
= 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);
(typeof(d) + ': ' + );
(typeof(a) + ': ' + );
(typeof(f) + ': ' + );
(typeof(s) + ': ' + );
(typeof(n) + ': ' + );
(typeof(b) + ': ' + );
(typeof(o) + ': ' + );
(typeof(person) + ': ' + );
Run $node to get
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 is a difference in detecting the return value of the object type using the typeof operator and the constucor attribute.
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 attribute returns the constructor function
Each variable has its constructor attribute, which not only provides whether this object is or not, but also provides what type of object this object is. In short, the constructor attribute saves a constructor pointing to an object, whether it is a custom or native type object.
One thing to note is that different browsers will detect regular expressions differently for typeof operators, and IE and Firefox will return 'object'.
Okay, that’s all for today’s content. If you have any questions, please leave a message below.