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
Real Estate | Homes | 3.5%
Real Estate | Homes | 3.5% FHA | Mortgage | Houses | 100% VA | Homes For Sale | Realtors | Zero Down USDA | Ontario Home | Ontario House | Half Percent Down | Hayward Home | Carson Home | 203 K FHA | Orange Realtors | Newark Realtors | Conventional Loans | Santa Ana Home | Anaheim House | Jumbo Loans | Anaheim Houses | Anaheim Realtors | City Down Payment | Walnut Creek Homes | Burbank Home | 2yr BK Discharge OK | Carson Homes | Stockton Home | <620 FICO with No 12 mo lates | Ontario Houses | Homes In Orange | 620+ FICO Ok | Elk Grove Home | Norwalk Home | 6% Seller Credit OK | Santa Rosa Home | Berkeley Home | 30 Yr Fixed | Costa Mesa Home | Davis Homes | 15 Yr Fixed | Napa Homes | Ontario MLS | 20 Yr Fixed | Pasadena Realtors | San Bernardino Home | 5 Yr Adjustable | Santa Clara Home | Campbell Home | 7 Yr Adjustable | Irvine Realtors | Modesto Home | 10 Yr Adjustable
The city of Berkeley
The city of Berkeley experienced an 8.23% drop in median sales price from last year going down from $751,333 (Dec. 08) to $689,505 (Dec. 09). In the city of Berkeley’s case, the decrease in median sales price spurred a dramatic reduction in median days on market and a comparable increase in number of units sold. Median days on market for a house in the city of Berkeley for December 08 was 43 days compared to 31 days in December 09 and number of units sold have increased from 39 units in Dec 08 to 51 units in Dec 09. It is apparent that the $60,000 drop in home prices caused a ripple in the Berkeley housing market. The drop in prices was enough to cut median days on market by 26.86% and alternatively drove up sales 30.77% by attracting home buyers with lower prices. A similar trend as Berkeley can be seen when analyzing home data for neighboring cities Walnut Creek and Castro Valley. Walnut Creek ca homes for sale saw their median prices slashed by almost 20% but like Berkeley, that in turn caused a drop in median days on market (23.08%) and an increase in number of units sold (5.26%). Same for Castro Valley ca homes for sale – drop in median home prices (8.01%) however a decrease in median days on market (46.08%) coupled with an increase in the number of units sold (26.32%).Antioch Ca Foreclosures | Home For Sale | 3.5% FHA | Home For Sale | Hayward Ca Foreclosures | 100% VA | Concord Ca Foreclosures | Oakland Ca Foreclosures | Zero Down USDA | Alameda Ca Foreclosures | Mountain View Ca Foreclosures | Half Percent Down | Pleasanton Ca Foreclosures | Stockton Ca Foreclosures | 203 K FHA | San Ramon Ca Foreclosures | Palo Alto Ca Foreclosures | Conventional Loans | Merced Ca Foreclosures | Modesto Ca Foreclosures | Jumbo Loans | Elk Grove Ca Foreclosures | Daly City Ca Foreclosures | City Down Payment | Redwood City Ca Foreclosures | Roseville Ca Foreclosures | 2yr BK Discharge OK | Sacramento Ca Foreclosures | Mortgage Calculator | <620 FICO with No 12 mo lates | Mortgage Calculator | Salinas Ca Foreclosures | 620+ FICO Ok | San Mateo Ca Foreclosures | Santa Clara Ca Foreclosures | 6% Seller Credit OK | Santa Rosa Ca Foreclosures | Sunnyvale Ca Foreclosures | 30 Yr Fixed | Napa Ca Foreclosures | Walnut Creek Ca Foreclosures | 15 Yr Fixed | San Bernardino Ca Foreclosures | Temecula Ca Foreclosures | 20 Yr Fixed | Santa Monica Ca Foreclosures | Rancho Cucamonga Ca Foreclosures | 5 Yr Adjustable | Riverside Ca Foreclosures | Vallejo Ca Foreclosures | 7 Yr Adjustable | Tracy Ca Foreclosures | Corona Ca Foreclosures | 10 Yr Adjustable
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
is known as the jeweler with the name of the popular mythology is to assume that all accepted pandora beads by all age groups pandora necklaces and do it. pandora bangles Pandora a nice name for the first lady pandora sets in the beautiful world, cheap pandora beads it means a lot to the world. cheap pandora necklaces So, as jewelry under the name Pandora, could not be bad.
Pandora magic, cheap pandora bangles like Pandora Jewelry. The first time I see you do not see the shiny surface, but can pandora silver sets his magic.From Pandora bracelets, Pandora's Pass, you his unique custom can feel feel unique pattern. discount pandora bead And never forget the charm. discount pandora necklace And that is discount pandora bangle good can be individually tailored to your personality with Pandora jewelry. discount pandora sets individual differences have different tastes in a lot of questions and jewelry is one of them.