gpt4 book ai didi

javascript - "myVar.constructor"和 "typeof myVar"有什么区别?

转载 作者:行者123 更新时间:2023-11-29 22:30:39 28 4
gpt4 key购买 nike

...之间有什么区别

if(myVar.constructor == String)

if(typeof myVar == "string")

最佳答案

表达式 myVar.constructor 只是返回成员“constructor”的值。给定对象具有指向字符串(或其他任何内容)的构造函数是完全可能且合法的。例如

function Apple() {}
var x = new Apple();
x.constructor = String
if (x.constructor == String) {
// True
}

尽管使用 typeof 运算符可以提供您要查找的信息

if (typeof myVar == 'string') {
// It's a string
}

关于javascript - "myVar.constructor"和 "typeof myVar"有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7100595/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com