Sunday, June 16, 2013

Private Members in JavaScript

function Book(title) {
    return {
        getTitle: function () {
            return title;
        }
    }
}
The returned object would just have an accessor called getTitle, which would return the argument, held in closure.
Crockford has a good page on Private Members in JavaScript - definitely worth a read to see the different options.

No comments:

Post a Comment