This file contains 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 greeting = "Hi there!"; // global scope | |
function sayHello() { | |
console.log(greeting); | |
} | |
sayHello(); // Prints "Hi there !" |
This file contains 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 myVariable = "Hi, I am a global variable"; | |
function sayHello() { | |
myVariable = "Hi, I am locally produced!"; | |
console.log(myVariable); | |
} | |
console.log(myVariable); // Prints "Hi, I am a global variable" | |
sayHello(); // Prints "Hi, I am locally produced!" | |
console.log(myVariable); // Prints "Hi, I am locally produced!" |
This file contains 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 myJob = "Developer"; | |
function showJob() { | |
var myJob = "Designer"; | |
console.log(myJob); | |
} | |
showJob(); // Prints "Designer" |
This file contains 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 myVariable = "global"; | |
printVariable(); // Prints "local" | |
function printVariable() { | |
var myVariable = "local"; | |
console.log(myVariable); | |
} | |
printVariable(); // Prints "local" |
This file contains 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 customer = { | |
firstName: "John", | |
lastName: "Doe", | |
greetCustomer: function(){ | |
// We use "this" instead of repeating "customer" | |
console.log("Hello again " + this.firstName + " " + this.lastName + "!"); | |
} | |
} | |
customer.greetCustomer(); // Prints "Hello again John Doe!" |
This file contains 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 customer = { | |
firstName: "John", | |
lastName: "Doe", | |
greetCustomer: function(){ | |
console.log("Hello again " + this.firstName + " " + this.lastName + "!"); | |
}, | |
whatIsMyObject: function() { | |
console.log(this); | |
} | |
} |
This file contains 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 customer = { | |
firstName: "John", | |
lastName: "Doe", | |
greetCustomer: function(){ | |
console.log("Hello again " + this.firstName + " " + this.lastName + "!"); | |
}, | |
whatIsMyObject: function() { | |
console.log(this); | |
}, | |
address: { |
This file contains 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 customer = { | |
firstName: "John", | |
lastName: "Doe", | |
greetCustomer: function(){ | |
console.log("Hello again " + this.firstName + " " + this.lastName + "!"); | |
}, | |
calculateAge: function(currentYear, birthDate){ | |
console.log(this.firstName + " is " + (currentYear - birthDate) + " years old."); | |
} | |
} |
This file contains 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 customer2 = { | |
firstName: "Jane", | |
lastName: "Smith" | |
} | |
customer.calculateAge.call(customer2, 2016, 1984); // Prints "Jane is 32 years old" | |
customer.calculateAge.apply(customer2, [2016, 1984]); // Prints "Jane is 32 years old" |
This file contains 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 customer = { | |
firstName: "John", | |
lastName: "Doe", | |
greetCustomer: function(){ | |
console.log("Hello again " + this.firstName + " " + this.lastName + "!"); | |
}, | |
calculateAge: function(currentYear, birthDate){ | |
console.log(this.firstName + " is " + (currentYear - birthDate) + " years old."); | |
} | |
} |