Paul
Paul

Reputation: 26650

Something is wrong in JavaScript object declaration

This code:

var doc = {
  foldPrompt: function(folded) {
    return folded ? "Click to unfold" : "Click to fold"
  },
  createFoldButtons: function() {
    var prompt = foldPrompt(true); //The error is here
    $("#ComparisonTable td.secrow").each(function(index, td){
      $(td).prepend($('<img src="minus.gif" class="foldbtn" alt="'+prompt+'" title="'+prompt+'">'));
    });
  }
}

gives me an error: Undefined variable: foldPrompt

What am I doing wrong?

Upvotes: 0

Views: 46

Answers (1)

cdhowie
cdhowie

Reputation: 169018

foldPrompt is not a variable; it's a property of doc, and you need an object reference to access properties of that object.

If someone calls doc.createFoldButtons(), then the this context variable will point at the same object that the doc variable does. So, replace foldPrompt(true) with this.foldPrompt(true).

Upvotes: 2

Related Questions