diff --git a/CodeExamples/Week3/javascriptClosures.html b/CodeExamples/Week3/javascriptClosures.html new file mode 100644 index 0000000000000000000000000000000000000000..2ae54d03d476e7c701790c7572f5c2fb4bda45b9 --- /dev/null +++ b/CodeExamples/Week3/javascriptClosures.html @@ -0,0 +1,58 @@ +<!DOCTYPE html> +<html lang="en-US"> + <head> + <title> + Javascript Skeleton + </title> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + </head> + <body> + <script> + window.firstName = 'Nikola' + window.lastName = 'Rasevic' + + let aleksandra = { + firstName: 'Aleksandra', + lastName: 'Rasevic', + useThatForThis + } + + //useThatForThis(printName) + aleksandra.useThatForThis(printName) + // aleksandra.useThatForThis(() => { + // console.log(`My names is ${this.firstName} ${this.lastName}`) + // }) + function useThatForThis(cb) { + //console.log('inside of function call') + //console.log(this) + let nikola = { + firstName: 'Nikola', + lastName: 'Rasevic', + innerFun + } + var that = this + function innerFun() { + // every function in js redefines its own "this" + console.log('inside innerFun') + console.log(this) + } + //call inner function with current context of 'this' + let boundInnerFun = innerFun.bind(nikola) + console.log('*********') + boundInnerFun() + innerFun.call(this) + innerFun() + console.log(that) + cb(that) + } + + function printName(that) { + console.log('Inside of printName') + console.log(this) + console.log(`My name is ${that.firstName} ${that.lastName}`) + } + //console.log('This is how I learned to write Javascript!') + </script> + </body> +</html>