it:ad:jscript:concepts:closures

IT:AD:JScript:Concepts:Closures

<callout type=“Navigation” class=“small”

>

<

/callout>

## Example ##

function Book(title) {
  var title_;

  this.getTitle = function() {
    return title_;
  };

  this.setTitle = function(title) {
    title_ = title;
  };

  // should use the setter in case it does something else than just assign
  this.setTitle(title);
}

var book = Book("War and Peace");

Compared to the prototype way:

function Book(title) {
  this.title = title;
}

Book.prototype.getTitle = function () {
  return this.title;
};

var myBook = new Book('War and Peace');

## Reference ##

Summary

  • /home/skysigal/public_html/data/pages/it/ad/jscript/concepts/closures.txt
  • Last modified: 2023/11/04 22:59
  • by 127.0.0.1