1.0.1 • Published 9 years ago

privatize v1.0.1

Weekly downloads
5
License
-
Repository
github
Last release
9 years ago

privatize

NPM Version

Build Status

Marks object properties as private. JFYI this module requires ES5 features.

Example

var privatize = require('privatize');

function A() {
    this.value = 123;
    
    setTimeout(function () {
        this.set(134); // Works
    }.bind(this), 1000);
    
    return privatize(this, 'set');
}

A.prototype = {
    set: function (value) {
        this.value = value;
    },
    
    get: function () {
        return this.value;
    }
};

var a = new A();
console.log(a.get()); // Works
a.set(111); // Error (Property is private)
a.set; // Error (Property is private)

Backbone example

var privatize = require('privatize'),
    Model = require('backbone').Model;

// Privatize all "write" methods
module.exports = privatize(new Model(), 'sync', 'set', 'fetch', 'attributes', 'clear', 'unset', 'save', 'destroy');