2

I've been looking through some library javascript source code and I've found a statement that I totally don't understand. This library is based on dojo and uses its implementation of "class inheritance". Here is a simplified version of code that is a subject:

dojo.declare("myCustomClass", {
    constructor:function(){
        // what does this statement mean?
        isContentShowing : false;

        //here some code that uses isContentShowing in callbacks
        dojo.connect(this, "fakeEvent", this, function(){
            if(this.isContentShowing){
            //do some stuff
            }
            //do more stuff
        });
    }
});

So the question is what does the isContentShowing: false; inside function body mean?

3
  • 1
    If that is literally the code, that's a syntax error. Commented Jun 4, 2012 at 7:33
  • 2
    @ChrisMorgan That's not. I think pst explained it quite well. Everybody forgets about labels nowadays... Commented Jun 4, 2012 at 7:43
  • 1
    @MaxArt: hmm... I thought JavaScript didn't support them. I know about them (and thought about them before adding the comment!), but thought they weren't in JavaScript. I stand corrected. Commented Jun 4, 2012 at 7:53

3 Answers 3

7

It's not a variable. It is a label:

Provides a statement with an identifier that you can refer to using a break or continue statement.

In this case, it is useless: it is not used as a label, it does not perform an assignment, and the result of evaluating false is discarded.

I suspect a = and this are desired as this.isContentShowing = false makes more sense given the conditional below. Perhaps the original author never ran into/realized this bug due to this.isContentShowing evaluating to undefined (and thus still being falsey, like, well, false) later on.

Or, as Chris suggests in a comment, the intent might have been:

dojo.declare("myCustomClass", {
  isContentShowing : false, // but , and not ;
  constructor:...
});
Sign up to request clarification or add additional context in comments.

5 Comments

Extremely confusing. Why putting false; right after that? I'm guessing it's some kind of bug...
@MaxArt It is confusing - I believe that it is indeed incorrect code.
@MaxArt - yes it's a bug, though as pst explained it's not a syntax error. The original coder most likely intended this.isContentShowing=false;.
Or perhaps isContentShowing: false, outside the function (inside the object literal).
@ChrisMorgan That could very well have been the intent, although as the ; would need to be a , in the object-literal... but in any case, as good an explanation for the [misguided] appearance as mine :-)
-1

This is called object literal in Javascript. It contains key value pair, e.g:

var car = {
    color: 'blue',
    price: 10000
}

so when you call car['color'] or car.color it will return blue. In the my example, color is the key and value is 'blue'. That's called key value pair.

Anyway, in the your example this.isContentShowing will return false by default.

1 Comment

In your answer you've shown an object literal. In the question the code is defining a function, not an object literal. In context it's fairly clear that the original coder meant this.isContentShowing = false;. In the question this.isContentShowing will return undefined, not false (though in the context of an if condition undefined will act the same as false).
-1

So it looks like a misplaced statement (interpreted instead as a label, as explained by @pst) That property should likely have been a peer of constructor: All properties on the object passed to dojo.declare are put on the Object prototype. Because an undefined value is falsey, in the end, the behavior is pretty much the same. Declaring falsey properties on the prototype is of limited value, unless someone is actually iterating through properties to check for existence, as dojo.parse does.

Comments

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.