public
Last active

Patrones basicos js

  • Download Gist
gistfile1.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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
//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);
}());
 
}({}));

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.