Note that there are some explanatory texts on larger screens.

plurals
  1. POPseudo-classical vs. "The JavaScript way"
    text
    copied!<p>Just finished reading Crockford's "<strong><a href="http://oreilly.com/catalog/9780596517748/" rel="noreferrer">JavaScript: The Good Parts</a></strong>" and I have a question concerning his stance on the psuedo-classical vs. prototypal approaches. Actually I'm not really interested in his stance; I just want to understand his argument so I can establish a stance of my own.</p> <p>In the book, Crockford seems to infer that constructor functions and "all that jazz" shouldn't be used in JavaScript, he mentions how the 'new' keyword is badly implemented - i.e. non-Constructor functions can be called with the 'new' keyword and vice versa (potentially causing problems).</p> <p>I thought I understood where he was coming from but I guess I don't.</p> <p>When I need to create a new module I would normally start of like this:</p> <pre><code>function MyModule(something) { this.something = something || {}; } </code></pre> <p>And then I would add some methods to its prototype:</p> <pre><code>MyModule.prototype = { setSomething : function(){}, getSomething : function(){}, doSomething : function(){} } </code></pre> <p>I like this model; it means I can create a new instance whenever I need one and it has its own properties and methods:</p> <pre><code>var foo = new MyModule({option1: 'bar'}); // Foo is an object; I can do anything to it; all methods of the "class" // are available to this instance. </code></pre> <p><strong>My question is</strong>: How do I achieve the above using an approach more suited to JavaScript? In other words, if "JavaScript" were a person, what would she suggest?</p> <p>Also: What does Crockford mean when he says a particular design pattern "is more expressive" then another?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload