[size=medium]Since constructor functions define new categories or classes of objects, the constructor property can help
determine the type of an object. For example, you might use code like the following to determine the type
of an unknown value:
The instanceof operator checks the value of the constructor property, so the code above could also be
written:
[/size]
determine the type of an object. For example, you might use code like the following to determine the type
of an unknown value:
if ((typeof o == "object") && (o.constructor == Date))
// Then do something with the Date object...The instanceof operator checks the value of the constructor property, so the code above could also be
written:
if ((typeof o == "object") && (o instanceof Date))
// Then do something with the Date object...[/size]
本文介绍了如何使用构造函数属性来确定JavaScript中对象的类型。通过检查对象的constructor属性或使用instanceof运算符,可以轻松判断一个未知值是否属于特定的类,例如Date对象。
7880

被折叠的 条评论
为什么被折叠?



