Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pseudo-classical vs. "The JavaScript way"

Tags:

javascript

Just finished reading Crockford's "JavaScript: The Good Parts" 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.

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).

I thought I understood where he was coming from but I guess I don't.

When I need to create a new module I would normally start of like this:

function MyModule(something) {
    this.something = something || {};
}

And then I would add some methods to its prototype:

MyModule.prototype = {
    setSomething : function(){},
    getSomething : function(){},
    doSomething : function(){}
}

I like this model; it means I can create a new instance whenever I need one and it has its own properties and methods:

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.

My question is: 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?

Also: What does Crockford mean when he says a particular design pattern "is more expressive" then another?

like image 987
James Avatar asked Apr 26 '09 16:04

James


3 Answers

See: Is JavaScript's “new” Keyword Considered Harmful?

It's important to remember that Crockford, like so many other JavaScript programmers, first approached the language with an eye toward "fixing" it - making it more like other (so-called "classical") OO languages. So a large amount of structural code was written, libraries and frameworks built, and... then they started to realize that it wasn't really necessary; if you approach JS on its own terms, you can get along just fine.

like image 149
Shog9 Avatar answered Oct 14 '22 03:10

Shog9


The prototypal variant for the example you have looks like the following in my understanding:

Object.beget = function (o) { /* Crockfords replacement for the new */ }

var myModule = {
    something : null,
    getSomething : function () {},
    setSomething : function () {},
    doSomething : function () {}
};

And then you can do:

var foo = Object.beget(myModule);
foo.something = bar;

UPDATE: You can also use the builder pattern to replace a constructor like this:

var myModuleBuilder = {
    buildMyModule : function (something) {
        var m = Object.beget(myModule);
        m.something = something || {};
        return m;
    }
}

so then you can do:

var foo = myModuleBuilder.buildMyModule(something);
like image 22
artemb Avatar answered Oct 14 '22 02:10

artemb


Your implementation is problematic because you're replacing the entire prototype object, losing the properties of the inherited function prototype and it would also break, or at least make it more difficult, the ability to make use of inheritance later on if you wrote other classes the same way.

A method more suited to Javascript would be:

var MyClass = function (storeThis) {
 this.storage = storeThis
}

MyClass.prototype.getStorage = function (){
   return this.storage;
}

MyClass.prototype.setStorage = function (newStorage){
  this.storage = newStorage;
}

Use it:

var myInstance = new MyClass("sup");
alert("myInstance storage: " + myInstance.getStorage());
myInstance.setStroage("something else");

As for the 'new' keyword and Crawford's problems with it, I can't really answer, because I haven't read the book, but I can see how you could create a new object by calling any function with the new keyword, including functions that are supposed to be methods of a class.

And when someone says something, such as a design pattern, is more 'expressive', he or she generally means that the design pattern is clear and simple to understand as to what it is achieving and how.

like image 24
Bjorn Avatar answered Oct 14 '22 04:10

Bjorn