Created
May 22, 2012 11:03
-
-
Save dlemmon/2768353 to your computer and use it in GitHub Desktop.
Patrones basicos js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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