Skip to content

Instantly share code, notes, and snippets.

@amhed
Created January 14, 2014 04:36
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 amhed/8413094 to your computer and use it in GitHub Desktop.
Save amhed/8413094 to your computer and use it in GitHub Desktop.
Ejemplo del constructor design pattern en javascript
var ConstructorName = (function() {
'use strict';
var variablePrivada = 0;
function ConstructorName(args) {
// enforces new
if (!(this instanceof ConstructorName)) {
return new ConstructorName(args);
}
// constructor body
this.miPropiedad = "Lo que yo quiera"; //Asi se retorna un objeto
}
ConstructorName.prototype.methodName = function(args) {
// Pero cualquier variable privada que yo haya declarado es
// visible desde este modulo, por lo que puedo usar la
// asignacion de metodos al objeto prototype y así no me sale
// el modelo "sucio" con todos los métodos cuando lo despliego en un debug screen
};
return ConstructorName;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment