Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Detect [[Class]] instead of using instanceof #54
A quick glance through Hoek’s source shows that most of the type detection is done through instanceof, eg. instanceof RegExp, etc.
This means that values from different contexts, eg. RegExps generated from a different realm through Node.js’s vm module would not be detected correctly, as the RegExp constructor is different and thus the instanceof check would fail.
Instead, you should use Object.prototype.toString, by doing something like:
var getClass = Object.prototype.toString; var className = getClass.call(object); console.log(className); // "[object RegExp]"
This method instead detects the [[Class]] attribute of objects, which are constant across contexts.