• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Modern JavaScript for the Impatient: Hard objects?

 
Ranch Hand
Posts: 109
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: 109
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!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic