• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Modern JavaScript for the Impatient: Hard objects?

 
Ranch Hand
Posts: 144
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cay,

In the Chapter 3, there is a "Hard Objects" topic.
It explains how to create Hard Objects in Javascript?

Thanks.
 
author
Posts: 284
35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It' an optional section, but I think it is pretty interesting. A "hard object" (so named by Douglas Crockford) uses closures to provide truly encapsulated objects. Here is an example.



Typical usage:



The object has methods, and it doesn't leak any implementation details, other than what the methods provide.

The object state consists of the variables that the createAccount closure captures.

Should you do this in your own code? Maybe not--although I have done it for one project and found it quite satisfying. And Crockford swears by it. But I think most programmers will prefer the class syntax, even if it doesn't give true encapsulation.

Cheers,

Cay
 
Jorge Ruiz-Aquino
Ranch Hand
Posts: 144
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, that's an amazing way of how to break the soft-class feature of Javascript. Although as you say, maybe "most programmers will prefer the class syntax".

I see this approach a bit complicated as the developer needs to take care of several things if he wants to do it right.

Anyways, it's always great to see how things can be done in a different way in Javascript.
Tahnks!
reply
    Bookmark Topic Watch Topic
  • New Topic