// SINGLETON PATTERN ****************************
var Foo = function () {
"use strict";
if (Foo._instance) {
//this allows the constructor to be called multiple times
//and refer to the same instance. Another option is to
//throw an error.
return Foo._instance;
}
Foo._instance = this;
//Foo initialization code
};
Foo.getInstance = function () {
"use strict";
return Foo._instance || new Foo();
}
var obj1 = Foo.getInstance();
var obj2 = Foo.getInstance();
console.log( obj1 === obj2);
// SINGLETON PATTERN ****************************
var Foo2 = (function () {
"use strict";
var instance; //prevent modification of "instance" variable
function Singleton() {
if (instance) {
return instance;
}
instance = this;
//Singleton initialization code
}
//instance accessor
Singleton.getInstance = function () {
return instance || new Singleton();
}
return Singleton;
}());
var obj21 = Foo2.getInstance();
var obj22 = Foo2.getInstance();
console.log( obj21 === obj22);
// MODULE PATTERN ****************************
var foo23 = (function () {
"use strict";
var instance;
function aPrivateFunction() { console.log("i m private method");}
return {
foo23: function(){
return instance = this
},
aPublicFunction: function () {
aPrivateFunction();
console.log("i m public method");
}
};
})();
//var myObj = new foo23();
foo23.customMethod = function(){console.log("m custom method");};
foo23.aPublicFunction();
//foo23.aPrivateFunction(); //error
foo23.customMethod();
// A function that generates a new function for adding numbers
function addGenerator(num) {
// Return a simple function for adding two numbers
// with the first number borrowed from the generator
return function (toAdd) {
return num + toAdd
};
}
// addFive now contains a function that takes one argument,
// adds five to it, and returns the resulting number
var addFive = addGenerator(5);
// We can see here that the result of the addFive function is 9,
// when passed an argument of 4
console.log(addFive(4)); // Which return true
var EmployeeModule = (function () {
var employeeList = [];
var allEmployee = function () {
return employeeList;
};
var add = function (employee) {
employeeList.push(employee);
};
var totalCount = function() {
return employeeList.length;
};
return {
add: add,
getAll: allEmployee,
totalCount: totalCount
};
} ());
function Animal() {
var walked = false
function walk() {
console.log('walking...')
walked = true
}
function hasBeenWalked() {
console.log(walked);
}
return {
walk: walk
, hasBeenWalked: hasBeenWalked
}
}
function Cat() {
var cat = {}
var walked = false
cat.__proto__ = Animal();
cat.pat = function pat() {
console.log('being patted')
}
cat.lasagna = function lasagna() {
console.log('Lasagna!')
walked = true
}
return cat
}
var garfield = Cat();
garfield.hasBeenWalked();
garfield.walk();
garfield.hasBeenWalked();
function fibo(n) {
var f = [];
for (var c = 0; c < n; ++c) {
f.push((c < 2) ? c : f[c-1] + f[c-2]);
}
console.log(f);
}
fibo(5);
function isPrime(i) {
for (var c = 2; c <= Math.sqrt(i); ++c)
if (i % c === 0) return false;
return true;
}
function primeList(N) {
var list = [];
for (var i = 2; i != N; ++i)
if (isPrime(i)) list.push(i);
console.log(list);
}
primeList(10);
function MySingletonClass() {
if ( arguments.callee._singletonInstance )
return arguments.callee._singletonInstance;
arguments.callee._singletonInstance = this;
this.Foo = function() {
// ...
}
}
var a = new MySingletonClass()
var b = MySingletonClass()
console.log( a === b ); // prints: true
var foo = {x: 10};
var bar = {
x: 20,
test: function () {
console.log(this === bar); // true
console.log(this.x); // 20
//this = foo; // error, can't change this value
console.log(this.x); // if there wasn't an error, then would be 10, not 20
}
};
bar.test();
foo.test = bar.test;
foo.test();
var a = {
x: 10,
calculate: function (z) {
console.log( this.x + this.y + z);
}
};
var b = {
y: 20,
__proto__: a
};
var c = {
y: 30,
__proto__: a
};
// call the inherited method
b.calculate(30); // 60
c.calculate(40); // 80
var x = 10;
(function foo() {
var y = 20;
(function bar() {
var z = 30;
// "x" and "y" are "free variables"
// and are found in the next (after
// bar's activation object) object
// of the bar's scope chain
console.log(x + y + z);
})();
})();
var Foo = function () {
"use strict";
if (Foo._instance) {
//this allows the constructor to be called multiple times
//and refer to the same instance. Another option is to
//throw an error.
return Foo._instance;
}
Foo._instance = this;
//Foo initialization code
};
Foo.getInstance = function () {
"use strict";
return Foo._instance || new Foo();
}
var obj1 = Foo.getInstance();
var obj2 = Foo.getInstance();
console.log( obj1 === obj2);
// SINGLETON PATTERN ****************************
var Foo2 = (function () {
"use strict";
var instance; //prevent modification of "instance" variable
function Singleton() {
if (instance) {
return instance;
}
instance = this;
//Singleton initialization code
}
//instance accessor
Singleton.getInstance = function () {
return instance || new Singleton();
}
return Singleton;
}());
var obj21 = Foo2.getInstance();
var obj22 = Foo2.getInstance();
console.log( obj21 === obj22);
// MODULE PATTERN ****************************
var foo23 = (function () {
"use strict";
var instance;
function aPrivateFunction() { console.log("i m private method");}
return {
foo23: function(){
return instance = this
},
aPublicFunction: function () {
aPrivateFunction();
console.log("i m public method");
}
};
})();
//var myObj = new foo23();
foo23.customMethod = function(){console.log("m custom method");};
foo23.aPublicFunction();
//foo23.aPrivateFunction(); //error
foo23.customMethod();
// A function that generates a new function for adding numbers
function addGenerator(num) {
// Return a simple function for adding two numbers
// with the first number borrowed from the generator
return function (toAdd) {
return num + toAdd
};
}
// addFive now contains a function that takes one argument,
// adds five to it, and returns the resulting number
var addFive = addGenerator(5);
// We can see here that the result of the addFive function is 9,
// when passed an argument of 4
console.log(addFive(4)); // Which return true
var EmployeeModule = (function () {
var employeeList = [];
var allEmployee = function () {
return employeeList;
};
var add = function (employee) {
employeeList.push(employee);
};
var totalCount = function() {
return employeeList.length;
};
return {
add: add,
getAll: allEmployee,
totalCount: totalCount
};
} ());
function Animal() {
var walked = false
function walk() {
console.log('walking...')
walked = true
}
function hasBeenWalked() {
console.log(walked);
}
return {
walk: walk
, hasBeenWalked: hasBeenWalked
}
}
function Cat() {
var cat = {}
var walked = false
cat.__proto__ = Animal();
cat.pat = function pat() {
console.log('being patted')
}
cat.lasagna = function lasagna() {
console.log('Lasagna!')
walked = true
}
return cat
}
var garfield = Cat();
garfield.hasBeenWalked();
garfield.walk();
garfield.hasBeenWalked();
function fibo(n) {
var f = [];
for (var c = 0; c < n; ++c) {
f.push((c < 2) ? c : f[c-1] + f[c-2]);
}
console.log(f);
}
fibo(5);
function isPrime(i) {
for (var c = 2; c <= Math.sqrt(i); ++c)
if (i % c === 0) return false;
return true;
}
function primeList(N) {
var list = [];
for (var i = 2; i != N; ++i)
if (isPrime(i)) list.push(i);
console.log(list);
}
primeList(10);
function MySingletonClass() {
if ( arguments.callee._singletonInstance )
return arguments.callee._singletonInstance;
arguments.callee._singletonInstance = this;
this.Foo = function() {
// ...
}
}
var a = new MySingletonClass()
var b = MySingletonClass()
console.log( a === b ); // prints: true
var foo = {x: 10};
var bar = {
x: 20,
test: function () {
console.log(this === bar); // true
console.log(this.x); // 20
//this = foo; // error, can't change this value
console.log(this.x); // if there wasn't an error, then would be 10, not 20
}
};
bar.test();
foo.test = bar.test;
foo.test();
var a = {
x: 10,
calculate: function (z) {
console.log( this.x + this.y + z);
}
};
var b = {
y: 20,
__proto__: a
};
var c = {
y: 30,
__proto__: a
};
// call the inherited method
b.calculate(30); // 60
c.calculate(40); // 80
var x = 10;
(function foo() {
var y = 20;
(function bar() {
var z = 30;
// "x" and "y" are "free variables"
// and are found in the next (after
// bar's activation object) object
// of the bar's scope chain
console.log(x + y + z);
})();
})();
No comments:
Post a Comment