Simple encapsulation in Javscript.

In the spirit of 'doing the simplest thing which works', I've been using the following idiom for encapsulating data within objects. I'm still quite new to Javascript, so I'm sure others have been doing similar things for a long time, in a more elegant fashion (I must get round to reading the jQuery source).

A naive object is:

var account = { name    : "fred"
              , balance : 100.00
              , branch  : "Worcester"
              } ;

My current favoured method for encapsulation is to use a constructor-function (not a javascript constructor, they're a bit nasty).

The constructor-function returns an interface object containing methods. Only these methods have access to the member variables held within the constructor-function's closure.

var new_account = function(name, branch) {
    // constructor-type code
    var _name    = name ;
    var _balance = 0.0 ;
    var _branch  = branch ;
 
    // create the *interface*
    var _interface ;
    _interface = { getName     : function()       { return _name ; }
                 , getBalance  : function()       { return _balance ; }
                 , getBranch   : function()       { return _branch ; }
                 // return of _iface below is to allow jQuery-style method chaining.
                 , credit      : function(amount) { _balance += amount ; return _iface ; } 
                 } ;
 
    // return the interface
    return _interface ;
}

This approach has problems with inheritance, but personally I have found inheritance to be unneeded in Javascript. It's loosely typed language - so interface inheritance is moot. Implementation inheritance is easily (and probably better) done as a has-a relation.

Comments

Tiffany & Co Shop specialises

Tiffany & Co Shop specialises in superior sterling silver Tiffany Jewelry, Tiffany Shop provides hundreds of discount and fashion Tiffany Jewellery,
tiffany jewellery
tiffany co
tiffany
GHD Styler Hair Straightener - shop online for ghd stylers, the new limited edition purple ghd IV styler & gift set, ghd hair care products,
cheap ghd
ghd straighteners
ghd pink
ghd
ghd Hair Straighteners
the women like ugg boots,ugg cardy boots,you could buy cheap ugg boots from here!
ugg boots
uggs

Abercrombie

High Waist Straight-leg pants outfit stunted jacket, showing slender eye-catching position, pertinent mixture of units Cheap Abercrombie and Fitch Outerwears Cheap Abercrombie and Fitch Outerwears discount Abercrombie and Fitch Outerwears discount Abercrombie and Fitch Outerwears Abercrombie and Fitch Outerwears sale Abercrombie and Fitch Outerwears sale buy Abercrombie and Fitch Outerwears buy Abercrombie and Fitch Outerwears Cheap Abercrombie Fitch Outerwears Cheap Abercrombie Fitch Outerwears discount Abercrombie Fitch Outerwears discount Abercrombie Fitch Outerwears Abercrombie Fitch Outerwears sale Abercrombie Fitch Outerwears sale buy Abercrombie Fitch Outerwears buy Abercrombie Fitch Outerwears Cheap Abercrombie Outerwears Cheap Abercrombie Outerwears discount Abercrombie Outerwears discount Abercrombie Outerwears Abercrombie Outerwears sale Abercrombie Outerwears sale buy Abercrombie Outerwears at Break; DSquared jeans on The patches, holes, stains a Less.