Private names use cases

Kam Kasravi kamkasravi at yahoo.com
Tue Dec 21 01:06:40 PST 2010


Coming from the commonjs perspective I did something similar to create private 
data per instance using a closure. 
I feed an object literal into a 'type' generator so that: 

Type.create('Foo', {
  imports: {
    log: "require('log')"
  },
  specification: {
    'private': { 
      bar: null, 
      baz: null
    },
    'constructor': function(props) {
      try {
        var properties = props || {};
        bar = properties.bar;
      } catch(e) {
        log.Logger.error(this,e);
      } 
      return this;
    },
    'public': { 
      print: function() {
        log.Logger.debug(this,'bar='+this.getBar());
      }
    } 
  }   
}, require, exports);

generates

// Foo
var Foo = (function() {
  // imports
  var log = require('log');
  // public
  Foo.prototype['print'] = function print() {
    log.Logger.debug(this, 'bar=' + this.getBar());
  };
  function Foo() {
    // private
    function privateData() {
      this.bar = null;
      this.baz = null;
    };
    var p_vars = new privateData();
    var bar = p_vars.bar;
    this.getBar = function getBar() {
      return bar;
    };  
    this.setBar = function setBar(b) {
      bar = b;
      return this;
    };
    var baz = p_vars.baz;
    this.getBaz = function getBaz() {
      return baz;
    };  
    this.setBaz = function setBaz(b) {
      baz = b;
      return this;
    };
    // constructor
    var args = Array.prototype.slice.call(arguments);
    arguments.callee.ctor = function(props) {
      try {
        var properties = props || {};
        bar = properties.bar;
      } catch(e) {
        log.Logger.error(this, e);
      }
      return this;
    };
    return arguments.callee.ctor.apply(this, args) || this;
  };
  return function() {
    var args = Array.prototype.slice.call(arguments);
    arguments.callee['constructor'] = Foo;
    return new Foo(args && args.length && args[0]);
  };
})();
exports.Foo = Foo;


the object literal looks a little like the object initializer strawman 
(http://wiki.ecmascript.org/doku.php?id=strawman:object_initialiser_extensions).
The idea is the user is shielded from private data lookup when creating an 
object instance.

eg  
Foo({bar:'hello'}).print(); // internally calls new
Foo({bar:'world'}).print();
      

________________________________
From: Garrett Smith <dhtmlkitchen at gmail.com>
To: Allen Wirfs-Brock <allen at wirfs-brock.com>
Cc: es-discuss at mozilla.org
Sent: Mon, December 20, 2010 11:28:15 PM
Subject: Re: Private names use cases

On 12/20/10, Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:
> I've seen mentions in the recent thread that the goal of the "Private Names"
> proposal was to support "private fields" for objects.  While that may be a
> goal of some participants in the discussion, it is not what I would state as
> the goal.
>
> I have two specific use cases in mind for "private names":
> 1) Allow JavaScript programmers, who choose to do so, to manage the direct
> accessibly of object properties.  This may mean limiting access to methods
> of a particular instance, or to methods of the same "class", or to various
> friends or cohorts, etc.

If private is based on lexical scope then it's already possible but it
can be a bad smell.

For example:

var Blah = function() {
  var privateBlah =  {
    complicatedDirtyWork : function() { alert("blah.") }
  };
  var Blah = {
    goBang : function() {
      privateBlah.complicatedDirtyWork();
    }
  };
  return Blah;
}();
Blah.goBang()

Great for a one-off, but when you wanna have many of the ADT (many
instances of Blah), then there's an equal number of privateBlah and
the strategy must make some sort of lookup, e.g.

  var delegateBlah = getPrivateBlah( this );
  delegateBlah.doTrick();

That's a bit more work and when there are only a couple of fields, it
seems like too much trouble.

If it were authored in a way that obviates that cruft but could still
be facilitated by using scope tricks, as above?
-- 
Garrett
_______________________________________________
es-discuss mailing list
es-discuss at mozilla.org
https://mail.mozilla.org/listinfo/es-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20101221/8e200761/attachment.html>


More information about the es-discuss mailing list