Skip to content

Instantly share code, notes, and snippets.

View mosijava's full-sized avatar

Mostafa Javaheripour mosijava

  • sport alliance
  • Hamburg Germany
View GitHub Profile
class Human {
constructor (gender,nationality){
this.gender = gender;
this.nationality = nationality;
}
humanIntro (){
var sex = this.gender == "female" ? "girl" : "boy";
return `I'm a ${sex} from ${this.nationality}`;
}
@mosijava
mosijava / closuresolution.js
Created July 27, 2018 12:11
a way to solve classic closure problem in javascript
var myFunctions= [];
function createMyFunction(i) {
return function() {
console.log("My value: " + i);
};
}
for (var i = 0; i < 3; i++) {
myFunctions[i] = createMyFunction(i);
@mosijava
mosijava / closureProblem.js
Created July 27, 2018 12:00
classic JavaScript closure problem
var myFunctions = [];
for (var i = 0; i < 3; i++) { // let's create 3 functions
myFunctions[i] = function() { // and store them in myFunctions
console.log("My value: " + i); // each should log its value.
};
}
for (var j = 0; j < 3; j++) {
myFunctions[j](); // and now let's run each one to see
}
@mosijava
mosijava / returnClosure2.js
Last active July 27, 2018 11:50
another cool example of closure
function outerFunction(x) {
var z = 3;
return function (y) {
x=x+1;//x++
alert(x + y + z);
}
}
var myVal = 2;
var innerFunction = outerFunction(myVal); // innerFunction is now a closures.
innerFunction(10); //will alert 16
@mosijava
mosijava / returnClosure.js
Created July 27, 2018 11:15
an example of closures to show the function can't de-alocate after returning closure
function outerFunction(x) {
var z = 3;
return function (y) {
alert(x + y + z);
}
}
var innerFunction = outerFunction(2); // innerFunction is now a closures.
innerFunction(10);
@mosijava
mosijava / simpleClosuer.js
Created July 27, 2018 10:49
a very simple closure example
function sayHelloToClosures(yourName) {
var text = 'Hello Closures from' + yourName;
var sayAlert = function() { alert(text); }
}