public
Created

Refactoring JavaScript

  • Download Gist
1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
var Memento = function(structure, config){
this.version = "0.4.0";
 
config = _.extend({ignore: []}, config);
 
var serializer = new Serializer(structure, config);
var mementoStack = new MementoStack(structure, config);
 
var restoreState = function (previousState, restoreConfig){
if (!previousState){ return; }
serializer.deserialize(previousState, restoreConfig);
};
 
this.store = function(){
var currentState = serializer.serialize();
mementoStack.push(currentState);
};
 
this.restore = function(restoreConfig){
var previousState = mementoStack.pop();
restoreState(previousState, restoreConfig);
};
 
this.restart = function(restoreConfig){
var previousState = mementoStack.rewind();
restoreState(previousState, restoreConfig);
};
};
2.js
JavaScript
1 2 3 4 5 6 7
var myObject = {
foo: "bar",
baz: function(){
// do stuff here
}
}
3.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14
var myObject = (function(){
 
var something = "what?";
 
return {
foo: "bar",
baz: function(){
// do stuff here
return something;
}
};
 
});
4.js
JavaScript
1 2 3 4
bind: function(view, options){
view.modelBinder = new ModelBinder(view, options);
view.modelBinder.bind();
}
5.js
JavaScript
1 2 3 4 5
unbind: function(view){
if (view.modelBinder){
view.modelBinder.unbind()
}
}

'new' in javascript? why? this isn't java or something

javascript is an object oriented language. use it. love it. get used to it, cause its not going anywhere anytime soon :)

... but seriously - object instances in javascript give you a lot of flexibility and capabilities. like anything else, you can take it too far. used with a sense of pragmatism, though, they can clean up your code significantly.

in case your interested, here's the blog post to go with these snippets: http://lostechies.com/derickbailey/2011/10/25/rebuilding-my-backbone-js-plugins-with-modules-srp-and-more/

Isn't it just as easy to do OOP in js without 'new' and 'this' keywords? Without 'new', the 'this' inside your object will point to the global window scope, causing funky problems that might not be easy to track down. If we don't use either 'new' or 'this', there's less chance for errs. It seems like 'new' fell to Occam's Razor.

var Person = function(name){
  var self = {};
  self.speak = function(){
    return 'hello, i am ' + name;
  };
  return self;
}

var dave = Person('dave');
dave.speak(); // ''hello, i am dave"

Absolutely, you can do that. I recommend doing that when it makes sense and I do that fairly regularly. Where it doesn't make sense is when you want to have code available to more than one instance, without having to re-process that code every time you create an instance.

In that code, it will re-create the speak function every time you call Person(). Every object that is returned from the Person method will have it's own copy of the speak method because that method is re-created every time. If you only have a few objects, no big deal. If you're dealing with a large number of objects, you have memory concerns and performance concerns to worry about. Additionally, we lose the benefit of prototypal inheritance with that code. If you wanted to replace the speak function on every Person object, you would have to manually replace it on every Person object.

But this is just object-oriented JavaScript. There are other paradigms that work equally as well in JavaScript, including functional programming. In what little spare time I have, I'm learning Haskel via http://learnyouahaskel.com - recommended site... great stuff... and I'm seeing how OO isn't always the right solution. It really comes down to trade offs and solutions. What solutions give you the flexibility you need, with tradeoffs that don't get in your way or at least stay out of your way as much as possible.

FWIW: I've read not quite half of "The Good Parts"... wish I could say the book lived up to it's name. Horrible writing style, with no real world examples in what I've read so far. Very dry, very boring. I do plan on finishing it, though.

If you're interested in the OO version of JavaScript - at least to get up to speed on that side of JS - check out http://www.amazon.com/JavaScript-Patterns-Stoyan-Stefanov/dp/0596806752

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.