Home > Others > JavaScript: Using Closure Space to Create Real Private Members

JavaScript: Using Closure Space to Create Real Private Members

I recently developed Angular Cloud Data Connector, which enables Angular developers to use cloud data, specifically Azure mobile service, using web standards like indexed DB. I was trying to create a way for JavaScript developers to embed private members into an object. My technique for this specific case is to use what I call “closure space”. In this tutorial, I want to share with you how to use this for your own projects and how performance and memory are impacted for the major browsers. But before diving into it, let me share why you may need private members, as well as an alternate way to “simulate” private members. Why Use Private Members? When you create an object using JavaScript, you can define value members. If you want to control read/write access on them, you need accessors that can be defined like this: 1 2 3 4 5 6 7 8 9 10 11 var entity = {}; entity._property = “hello world”; Object.defineProperty(entity, “property”, { get: function () { return this._property; }, set: function (value) { this._property = value; }, enumerable: true, configurable: true }); Doing this, you have full control over read and write operations. The problem is that […]

Categories: Others Tags:
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.