Skip to content
Snippets Groups Projects
Commit 2fd169e7 authored by Andrej Rasevic's avatar Andrej Rasevic
Browse files

adding js closure example

parent 6dc8c2d3
No related branches found
No related tags found
No related merge requests found
<!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>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment