最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to check a null value with an extension method using Object.prototype - Stack Overflow

matteradmin10PV0评论

There are a variety of places where I need to check if a JavaScript variable is null or empty string so I wrote an extension method that looks like this:

Object.prototype.IsNullOrEmptyString = function()
{
    return (this == null || (typeof this === "string" && this.length == 0));
}

I then call it like so:

var someVariable = null;

if (someVariable.IsNullOrEmptyString())
    alert("do something");

But that doesn't work because, at the point of evaluation in the if statement, someVariable is null. The error I keep getting is "someVariable is null".

You can see it live here: / (run in Firefox and notice the error console)

Is there anyway to do what I want and have a single extension method check for null and other things at the same time?

There are a variety of places where I need to check if a JavaScript variable is null or empty string so I wrote an extension method that looks like this:

Object.prototype.IsNullOrEmptyString = function()
{
    return (this == null || (typeof this === "string" && this.length == 0));
}

I then call it like so:

var someVariable = null;

if (someVariable.IsNullOrEmptyString())
    alert("do something");

But that doesn't work because, at the point of evaluation in the if statement, someVariable is null. The error I keep getting is "someVariable is null".

You can see it live here: http://jsfiddle/AhnkF/ (run in Firefox and notice the error console)

Is there anyway to do what I want and have a single extension method check for null and other things at the same time?

Share Improve this question asked Nov 3, 2011 at 15:17 sohtimsso1970sohtimsso1970 3,2865 gold badges30 silver badges39 bronze badges 1
  • Extending Object is not a good idea. It should be considered "final" – Bakudan Commented Nov 3, 2011 at 15:40
Add a ment  | 

3 Answers 3

Reset to default 3

null does not have any properties or methods. You have to create a function, and pass the variable, so it can be tested. Note: To test whether a variable is really an empty string, using === "" is remended.*

function isNullOrEmpty(test) {
    return test === null || test === "";
}

var someVariable = null;
if (isNullOrEmpty(someVariable)) alert("Do something");

* about == and ===. The following parisons are true:

null == undefined
"" == 0;
"" == false
"" == 

Because your variable is not an object, so its not working. Do it this way.

Object.prototype.IsNullOrEmptyString = function(obj)
{
    return (obj == null || (typeof this === "string" && this.length == 0));
}


if(Object.IsNullOrEmptyString(null))
  alert('yes');

just make it a function call.

function IsEmptyString(value)
{
    return (value == null ||
            value === "");
}

Here is a fiddle

Edit: consolidated (=== undefined and === null) into (== null)

Post a comment

comment list (0)

  1. No comments so far