1

I have a function, which receives 2 params: a variable, and a string representation of a data type ('String', 'Object' etc):

function typeChecker(variable, dataType) {
    // checks type, returns true or false
}

I want to convert the second parameter to a constructor, so that this expression does not throw an error:

variable instanceof 'Date'

Question: Is it possible to convert any of these:

'String'
'Date'
'Object'

To these:

String
Date
Object
5
  • 1
    You could use eval if it's not dangerous input Commented Aug 16, 2018 at 18:32
  • 1
    Does the dataType have to be a string, because parameters can be constructors, so typeChecker({}, Object) is possible Commented Aug 16, 2018 at 18:33
  • @SampsonCrowley If it is, how to figure it out in order to throw an error? Commented Aug 16, 2018 at 18:34
  • 1
    @Eduard you'd have to only accept params from a whitelist. Using bracket notation on window or global is a better option Commented Aug 16, 2018 at 18:36
  • @SebastianSpeitel I was thinking about that, but have not dared to implement this way, trying to be cautious in order not to end up in some unexpected behavior. Besides, for primitive data types I use typeofsince was not sure about performance implications of using instanceof when not necessary. Commented Aug 16, 2018 at 18:37

2 Answers 2

3

Those constructors all happen to be members of the global object (either window in the browser or global in Node.js), so you could do one of

variable instanceof window['Date']
variable instanceof global['Date']

If your constructor does not exist as a member o the global object, you can check if any prototype in the value's prototype chain is associated with a constructor whose name matches the desired string:

function checkIfValueIsOfTypeName(value, typeName) {
    while(value = Object.getPrototypeOf(value)) {
        if(value.constructor && value.constructor.name === typeName) {
            return true;
        }
    }
    return false;
}

This is more or less how instanceOf operates internally, except instanceOf directly compares constructor to the right-hand value, instead of comparing its name to a string, which is what you want to do.

Sign up to request clarification or add additional context in comments.

2 Comments

I assume it is safe to use window object in this case?
@Eduard Yes, the window object is safe to use both in terms of availability (it is globally available) and security (unlike eval which could cause arbitrary code execution that you didn't intend).
0

You can use typeof

console.log(typeof 10);
// output: "number"

console.log(typeof 'name');
// output: "string"

console.log(typeof false);
// output: "boolean"

1 Comment

Not for composite data types. This is why ìnstanceof is used in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.