privatize

1.0.1 • Public • Published

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');

Package Sidebar

Install

npm i privatize

Weekly Downloads

1

Version

1.0.1

License

none

Last publish

Collaborators

  • azproduction