0

I want return an empty object meanwhile the object equals to boolean false in my function. Obviously,if I return {} like the code below,it equals to boolean true which cannot meet my request.

decodeAndParse: function (jsonStr) {
  if (this.NaEptStr(jsonStr)) {
    var result;

    result = decodeURIComponent(jsonStr);
    result = JSON.parse(result);

    return result;
  }
  return false;
}

Is there any possible to solve this problem?

very thankful if anybody can help me solve this problem

6
  • Why not just test for false or an empty object? Commented Jul 29, 2014 at 9:06
  • 1
    why don't you have a key in object and make decisions on that like if (this.NaEptStr(jsonStr)) { result.status=true; return result}else{return {status:false}} Commented Jul 29, 2014 at 9:06
  • that's really a good way thx. :) @Mritunjay Commented Jul 29, 2014 at 9:57
  • if you want this as an answer let me know. Commented Jul 29, 2014 at 9:58
  • @Mritunjay—but that requires modifying the returned object and how it is evaluated. There are alternatives that don't require the object to be modified, just how it's evaluated. Commented Jul 29, 2014 at 23:55

2 Answers 2

2

Your code can be:

decodeAndParse: function (jsonStr) {
  return this.NaEptStr(jsonStr)? JSON.parse(decodeURIComponent(jsonStr)) : {};
}

Then in the calling code you probably have something like:

var obj = foo.decodeAndParse(jsonStr);

if (obj) {
  ...
}

You could have a simple "emptyObj" test like:

function isEmptyObject(obj) {
  return !!Object.keys(obj).length;
}

Then in the caller:

if (isEmptyObject(obj)) {

If you need to shim Object.keys, see MDN. Note that IE8 still has a significant user share (greater than 20% of desktop browsers according to netmarketshare) so don't ignore non–ES5 browsers.

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

3 Comments

anyone running IE8 must still be running XP, officially EOL :(
so,an empty object or false,I can only reach one in the last return expression.
@user2297823–it depends on how the returned value is evaluated, e.g. if (returnedValue) will always evaluate to true if returnedValue is an object (even a Boolean object with a value of false) per the rules in ECMA-262. You can't change that. So you need to evaluate the returned value differently using one of the methods suggested in the answers (or others).
0

make a function that counts keys in object

function isNotEmpty(obj){
    var count = 0;
    for (var k in obj) {
        if (obj.hasOwnProperty(k)) {
           ++count;
        }
    }
   return count ? true : false;
}

and modify your if to

   if (isNotEmpty(this.NaEptStr(jsonStr)))

But the simplest way to check if required field in object is present

 if(this.naEpStr(jsonStr).myRequiredField)

6 Comments

Yes, of course it is, so long as you're competent enough to use shims to support crappy old MS browsers with ancient JS implementations...
kangax.github.io/compat-table/es5/#Object.keys sometimes IE8 is still required =D but you're right
More to the point, my mantra with JS is to code to modern standards and fix the browsers with shims. Don't write suboptimal code adhering to old standards.
There's no need to count the keys, just return true if there are any own properties and false otherwise.
@RobG that's a fair comment - it would indeed be simpler to just return true in the if branch.
|

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.