Last active
September 22, 2019 08:36
-
-
Save ZeVS777/53d98a6e4dc2f25ba211c77923b954e8 to your computer and use it in GitHub Desktop.
Javascript patterns
This file contains hidden or 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
var Class = (function () { | |
"use strict"; | |
//Private property | |
var variable = 5; | |
//Constructor | |
function ClassInstance(param) { | |
if(!(this instanceof ClassInstance)){ | |
return; | |
} | |
//Public property | |
this.property = (param === undefined || param == null) ? variable : param; | |
//Readonly property | |
Object.defineProperty(this, "readonly", { | |
value: 10, | |
writable: false | |
}); | |
//GetterSetter properties | |
Object.defineProperties(this, { | |
gettersetter: { | |
get: function() { | |
return variable; | |
}, | |
set: function(value) { | |
variable = value; | |
} | |
}, | |
getter: { | |
get: function() { | |
return variable; | |
} | |
} | |
}); | |
} | |
//Public function | |
ClassInstance.prototype.print = function(){ | |
print(this); | |
} | |
//Private function | |
function print(self){ | |
console.log(self.property); | |
} | |
return ClassInstance; | |
}()); | |
var c1 = new Class(); | |
c1.print(); //5 | |
var c2 = new Class(9); | |
c2.print(); //9 | |
c1 = c2; | |
c1.print(); //9 | |
c1.property = 7; | |
c2.print(); //7 | |
console.log(c1.readonly); //10 | |
c1.readonly = 6; //in strict mode error | |
console.log(c1.readonly); //10 | |
c1.gettersetter = 11; | |
console.log(c1.gettersetter); //11 | |
console.log(c1.getter); //11 |
This file contains hidden or 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
var Controller = function(param){ | |
"use strict"; | |
if(!(this instanceof Controller)){ | |
return; | |
} | |
var self = this, | |
variable = 5; | |
self.property = (param === undefined || param == null) ? variable : param; | |
self.print = print; | |
function print(){ | |
console.log(format()); | |
} | |
function format(){ | |
return "property = " + self.property | |
} | |
}; | |
var c = new Controller(7); | |
c.print(); //property = 7 | |
c.property = 10; | |
c.print(); //property = 10 | |
var c1 = new Controller(); | |
c1.print(); //property = 5 |
This file contains hidden or 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
var Factory = (function(){ | |
var variable = 5; | |
return { | |
print: print | |
} | |
function print(){ | |
console.log(format()); | |
} | |
function format(){ | |
return "property = " + variable; | |
} | |
}()); | |
Factory.print(); //property = 5; |
This file contains hidden or 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
(function(root, factory){ | |
root.ReadOnlyFactory = factory(); | |
}(this, function() { | |
var variable = 5; | |
var ReadOnlyFactory = (function(){ | |
return { | |
print: print | |
} | |
function print(){ | |
console.log(format(variable)); | |
} | |
function printProperty(){ | |
console.log(format()); | |
} | |
function format(){ | |
return "property = " + variable; | |
} | |
}()); | |
//Readonly property | |
Object.defineProperty(ReadOnlyFactory, "readonly", { | |
value: 10, | |
writable: false | |
}); | |
//GetterSetter properties | |
Object.defineProperties(ReadOnlyFactory, { | |
gettersetter: { | |
get: function() { | |
return variable; | |
}, | |
set: function(value) { | |
variable = value; | |
} | |
}, | |
getter: { | |
get: function() { | |
return variable; | |
} | |
} | |
}); | |
return ReadOnlyFactory; | |
})); | |
ReadOnlyFactory.print(); //property = 5 | |
console.log(ReadOnlyFactory.getter); //5 | |
ReadOnlyFactory.gettersetter = 7; | |
ReadOnlyFactory.print(); //property = 7 | |
console.log(ReadOnlyFactory.readonly); //10 |
This file contains hidden or 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
var Service = (function(){ | |
"use strict"; | |
var service = (function(){ | |
var self; | |
var pauseProcess = false; | |
function serviceInstance(){ | |
self = this; | |
self.counter = 0; | |
print(); | |
} | |
serviceInstance.prototype.pause = function(){ | |
pauseProcess = !pauseProcess; | |
if(!pauseProcess){ | |
print(); | |
} | |
} | |
function print(){ | |
console.log("counter = " + self.counter); | |
setTimeout(function(){ | |
if(!pauseProcess){ | |
self.counter++; | |
print(); | |
} | |
}, 1000); | |
} | |
return serviceInstance | |
}()); | |
return new service(); | |
}()); | |
//counter = 0 | |
//counter = 1 | |
//counter = 2 | |
var t = setTimeout(function(){ | |
Service.pause(); | |
console.log(Service.counter); //2 | |
}, 3000); |
This file contains hidden or 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
var SingletonClass = (function(){ | |
"use strict"; | |
var self, | |
variable = 5; | |
//Constructor | |
function SingletonClassInstance(param){ | |
if(!(this instanceof SingletonClassInstance)){ | |
return; | |
} | |
if(self) { | |
console.log("singleton"); | |
return self; | |
} | |
console.log("constructed"); | |
self = this; | |
this.property = (param === undefined || param == null) ? variable : param; | |
} | |
SingletonClassInstance.prototype.print = print; | |
function print(){ | |
console.log(format()); | |
} | |
function format(){ | |
return "property = " + self.property; | |
} | |
return SingletonClassInstance; | |
}()); | |
var c1 = new SingletonClass(10); //constructed | |
c1.print(); //property = 10 | |
var c2 = new SingletonClass(); //singleton | |
c2.print(); //property = 10 |
This file contains hidden or 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
(function(root, factory){ | |
root.SingletonController = factory(); | |
}(this, function(){ | |
"use strict"; | |
var self; | |
var SingletonController = function(param){ | |
if(!(this instanceof SingletonController)){ | |
return; | |
} | |
if(self) { | |
console.log("singleton"); | |
return self; | |
} | |
var variable = 5; | |
self = this; | |
self.property = (param === undefined || param == null) ? variable : param; | |
self.print = print; | |
function print(){ | |
console.log(self.property); | |
} | |
}; | |
return SingletonController; | |
})); | |
var c = new SingletonController(7); | |
c.print(); //property = 7 | |
c.property = 10; | |
c.print(); //property = 10 | |
var c1 = new SingletonController(); //singleton | |
c1.print(); //property = 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment