Skip to content

Instantly share code, notes, and snippets.

@dlemmon
Created May 22, 2012 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlemmon/2768353 to your computer and use it in GitHub Desktop.
Save dlemmon/2768353 to your computer and use it in GitHub Desktop.
Patrones basicos js
//clase tipica
var Dog = function(name) {
this.name = name;
};
Dog.prototype.legs = 4;
Dog.prototype.speak = function() {
return "Un perro de " + this.legs + " patas llamado " + this.name + " dice guau!";
};
//closure factory
var newDog = function(name) {
var legs = 4;
return {
speak: function() {
return "Un perro de " + legs + " patas llamado " + name + " dice guau!";
}
};
};
//singleton
var System = {
prop: null,
dog: null,
charlie: null,
init: function() {
prop = "test";
dog = new Dog('Hoover');
charlie = newDog('Charlie');
},
print: function() {
console.log(prop);
console.log(dog.speak());
console.log(charlie.speak());
}
};
// functiones autoejecutables
engine = (function(p) {
(p.setup = function() {
System.init();
}());
(p.dr = function() {
System.print(p);
}());
}({}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment