admin管理员组文章数量:1327134
In this question I did not see suggestions to use constructor.
So instead of typeof callback == "function"
I would use callback && (callback.constructor==Function)
.
To me it seems obvious that parison to memory pointers is always better than parison to strings in terms of both runtime performance and coding safety.
Why not use constructor to detect all types and forget about ugly typeof
?
It works for all primitive types, functions and arrays:
undefined === undefined
null === null
[1,2,3].constructor == Array
(1).constructor == Number
(true).constructor == Boolean
(()=>null).constructor == Function
'abc'.constructor == String
(new Date()).constructor == Date
else it's an object, where instanceof helps to detect it's parents if needed.
If string interning can be relied upon then runtime performance advantage goes away. But safe coding advantage still stays.
In this question I did not see suggestions to use constructor.
So instead of typeof callback == "function"
I would use callback && (callback.constructor==Function)
.
To me it seems obvious that parison to memory pointers is always better than parison to strings in terms of both runtime performance and coding safety.
Why not use constructor to detect all types and forget about ugly typeof
?
It works for all primitive types, functions and arrays:
undefined === undefined
null === null
[1,2,3].constructor == Array
(1).constructor == Number
(true).constructor == Boolean
(()=>null).constructor == Function
'abc'.constructor == String
(new Date()).constructor == Date
else it's an object, where instanceof helps to detect it's parents if needed.
If string interning can be relied upon then runtime performance advantage goes away. But safe coding advantage still stays.
Share Improve this question edited May 23, 2017 at 12:26 CommunityBot 11 silver badge asked Oct 1, 2016 at 2:43 alpavalpav 3,0624 gold badges40 silver badges49 bronze badges 1-
Comparing
function
withfunction
? – Rayon Commented Oct 1, 2016 at 2:45
2 Answers
Reset to default 4instanceof
is better because it works with inherited constructors. .constructor
is a mutable property on an object, so it's not a good thing to check because one can simply change it. You can't change the instanceof
something.
const x = new Date();
console.log("Date Constructor", x.constructor);
x.constructor = "herpderpderp";
console.log("Date Constructor", x.constructor);
You can also define your own functions for both tests that also work on primitives by using getPrototypeOf and isPrototypeOf. E.G.:
function typeOf(obj) {
return Object.getPrototypeOf(obj).constructor;
}
typeOf(2) === Number // true
typeOf("cats") === String // true
class Foo {}
typeOf(new Foo()) === Foo // true
class Bar extends Foo {}
typeOf(new Bar()) === Bar // true
typeOf(new Bar()) === Foo // false
var b = new Number(3)
if (typeOf(b) === Number) {
console.log(b.valueOf() + 5)
}
and
function instanceOf(obj, type) {
var objType = typeOf(obj)
return (
// Allow native instanceof in case of Symbol.hasInstance
obj instanceof type ||
// Handle case where is of type type
typeOf(obj) === type ||
// Handle general case
type.isPrototypeOf(objType) ||
// Handle special case where type.prototype acts as a
// prototype of the object but its type isn't in the
// prototype chain of the obj's type
// OPTIONALLY remove this case if you don't want
// primitives to be considered instances of Object
type.prototype.isPrototypeOf(objType.prototype)
);
}
instanceOf(3, Number) // true
instanceOf(new Number("2"), Number) // true
instanceOf(2, Number) // true, OPTIONAL with the last condition
// but is probably preferable as 2 does
// indeed get all methods of Objects
class Hat {}
instanceOf(new Hat(), Hat) // true
class Fedora extends Hat {}
instanceOf(new Fedora(), Fedora) // true
instanceOf(new Fedora(), Hat) // true
instanceOf(new Fedora(), Object) // true
本文标签: constructor vs typeof to detect type in JavaScriptStack Overflow
版权声明:本文标题:constructor vs typeof to detect type in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742199316a2431662.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论