我可以使用 constructor.name 来检测 JavaScript 中的类型吗

发布于 2024-11-29 09:33:04 字数 517 浏览 5 评论 0原文

我可以使用“构造函数”属性来检测 JavaScript 中的类型吗? 或者有什么我应该知道的事情吗?

例如:var a = {}; a.构造函数.名称; // 输出“Object”

var b = 1; b.构造函数名称; // 输出“Number”

var d = new Date(); d.构造函数名称; // 输出“Date”而不是 Object

var f = new Function(); f.构造函数.名称; 输出“Function”而不是 Object

//仅当在参数上使用时才 arguments.constructor.name; //像第一个示例一样输出对象

我经常看到开发人员使用:Object.prototype.toString.call([])

Object.prototype.toString.call({} )

Can I use the "constructor" property to detect types in JavaScript?
Or is there something I should know about it.

For example: var a = {}; a.constructor.name; // outputs "Object"

or var b = 1; b.constructor.name; // outputs "Number"

or var d = new Date(); d.constructor.name; // outputs "Date" not Object

or var f = new Function(); f.constructor.name; // outputs "Function" not Object

only if use it on arguments arguments.constructor.name; //outputs Object like first example

I see quite often developers using: Object.prototype.toString.call([]) or

Object.prototype.toString.call({})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

路弥 2024-12-06 09:33:04

您可以使用 typeof,但它返回 误导 有时会出现结果。相反,请使用 Object.prototype.toString.call(obj),它使用对象的内部 [[Class]] 属性。您甚至可以为它创建一个简单的包装器,因此它的行为类似于 typeof

function TypeOf(obj) {
  return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
}

TypeOf("String") === "string"
TypeOf(new String("String")) === "string"
TypeOf(true) === "boolean"
TypeOf(new Boolean(true)) === "boolean"
TypeOf({}) === "object"
TypeOf(function(){}) === "function"

不要使用 obj.constructor 因为它会被更改,尽管您可能< /em> 可以使用 instanceof 来查看是否正确:

function CustomObject() {
}
var custom = new CustomObject();
//Check the constructor
custom.constructor === CustomObject
//Now, change the constructor property of the object
custom.constructor = RegExp
//The constructor property of the object is now incorrect
custom.constructor !== CustomObject
//Although instanceof still returns true
custom instanceof CustomObject === true

You can use typeof, but it returns misleading results sometimes. Instead, use Object.prototype.toString.call(obj), which uses the object's internal [[Class]] property. You can even make a simple wrapper for it, so it acts similar to typeof:

function TypeOf(obj) {
  return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
}

TypeOf("String") === "string"
TypeOf(new String("String")) === "string"
TypeOf(true) === "boolean"
TypeOf(new Boolean(true)) === "boolean"
TypeOf({}) === "object"
TypeOf(function(){}) === "function"

Don't use obj.constructor because it be changed, although you might be able to use instanceof to see if it is correct:

function CustomObject() {
}
var custom = new CustomObject();
//Check the constructor
custom.constructor === CustomObject
//Now, change the constructor property of the object
custom.constructor = RegExp
//The constructor property of the object is now incorrect
custom.constructor !== CustomObject
//Although instanceof still returns true
custom instanceof CustomObject === true
椵侞 2024-12-06 09:33:04

您可以使用typeof
例如:typeof("Hello")

You can use typeof
Ex: typeof("Hello")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文