Can I extend the undefined object like any other object?

前端 未结 1 556
遥遥无期
遥遥无期 2021-01-22 11:01

I want to create a method for the String object that checks if it\'s undefined:

String.prototype.isDefined = (str) => typeof str === \'undefined\         


        
1条回答
  •  伪装坚强ぢ
    2021-01-22 11:13

    No, you can't. undefined is not an object, it's a value.

    You can't possibly write a function that checks if a string is undefined. If it's undefined, it's not a string. It's undefined.

    I don't see what is not clean or readable with

    function isDefined(x) { return x !== undefined; }
    

    0 讨论(0)
提交回复
热议问题