Skip to content

Instantly share code, notes, and snippets.

View date_0.js
var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day [,hour,minute,second,millisecond ]);
View closure7.js
for (var i = 0; i < 10; i++) {
(function (j) {
setTimeout(function() {
console.log(j);
}, j*1000);
})(i);
}
View closure6.js
for (var i = 0; i < 10; i++) {
function timer(j) {
setTimeout(function() {
console.log(j);
}, j*1000);
};
timer(i);
View closure5.js
for (var i = 0; i < 10; i++) {
console.log(i);
setTimeout(function() {
console.log(i);
}, i*1000);
}
View closure4.js
function bakeCake(ingredient){
// Code that adds the ingredient to the cake batter
console.log(ingredient+ ' cake : add ' +ingredient+ ' to the batter');
function ovenTemperature(temperature, time){
// Code that sets the right temperature for baking the cake
console.log('Set the oven temperature to ' +temperature+' and ready to bake the '+ingredient+' cake for ' +time+ ' minutes');
}
return ovenTemperature;
}
View closure3.js
console.log(myFunction());
// Prints
function add(number2) {
return number1 + number2;
}
View closure2.js
function myFunction(number1) {
function add(number2) {
/* The number1 variable is accessible from this function,
because number1 has been defined outside of the add function */
return number1 + number2;
}
View closure1.js
for (var i = 0; i < 10; i++) {
setTimeout(function() {
console.log(i);
}, i*1000);
}
View this_7.js
var customer = {
firstName: "John",
lastName: "Doe",
greetCustomer: function(){
console.log("Hello again " + this.firstName + " " + this.lastName + "!");
function nestedFunction(){
console.log(this);
}
nestedFunction();
View this_6.js
var cat = "Alphonse";
// The variable cat is actually attached to the global window object :
console.log(window.cat === cat); // Prints true
// "this" in the global context has the value of the global window object
console.log(this); // Prints the window object