Created
July 20, 2014 23:15
-
-
Save elreimundo/f33e0a5e52f450d2b043 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// we're starting this function with an IIFE http://benalman.com/news/2010/11/immediately-invoked-function-expression/ | |
// it's important to start this with a semicolon because otherwise any previously loaded JavaScript might try and invoke | |
// its last line using the anonymous function that we define here as its argument, and then invoke the resulting return value | |
// on line 63. That would be silly, yo. So we explicitly end the previous thought with a semicolon. | |
;(function () { | |
// CLOSURES | |
function captureWhatNumberIsRightNow (number) { | |
return function () { | |
return number; | |
} | |
} | |
var number = 6 | |
itWasSix = captureWhatNumberIsRightNow(number); | |
number = 5 | |
itWasFive = captureWhatNumberIsRightNow(number); | |
itWasTwelve = captureWhatNumberIsRightNow(12); | |
window.number = 142 | |
console.log(itWasSix()) | |
console.log(itWasFive()) | |
console.log(itWasTwelve()) | |
// THIS | |
function introduceYoSelf () { | |
console.log('my name is ' + this.name); | |
} | |
function Person (name) { | |
this.name = name; | |
} | |
Person.prototype.sayHi = introduceYoSelf | |
johnLennon = new Person('John') | |
johnLennon.sayHi() | |
ringoStarr = new Person('Ringo') | |
ringoStarr.sayHi() | |
introduceYoSelf() | |
function whatIsThis () { | |
console.log(this); | |
} | |
whatIsThis.call(ringoStarr) | |
whatIsJohn = whatIsThis.bind(johnLennon) | |
whatIsThis() | |
Person.prototype.addAButtonToTheScreen = function () { | |
var button = document.createElement('button') | |
document.body.appendChild(button) | |
button.innerText = this.name | |
button.addEventListener('click', this.sayHi.bind({name: 'Sherif is super awesome'})); | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment