Created
October 6, 2014 19:34
-
-
Save bigeasy/ae1bfcb242a961ade5be to your computer and use it in GitHub Desktop.
`this` manipulation in JavaScript
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 slice = [].slice | |
var array = [] | |
console.log(array.push) | |
var object = { | |
f: function (x, y) { | |
var vargs = slice.call(arguments) | |
console.log(this.i, x, y) | |
}, | |
i: 2 | |
} | |
object.f(1, 3) | |
var f = object.f | |
/* nothing_here. */ f(1, 3) // <- ? `undefiend` | |
f.call(object, 1, 3) | |
f.apply(object, [ 1, 3 ]) | |
var a = [] | |
a[0] = 1 | |
a = a.concat(3) | |
f.apply(object, a) | |
var thingamabob = { | |
f: f.bind(object, 7), | |
i: 4 | |
} | |
function sum (a, b) { return a + b } | |
var add7 = sum.bind(null, 7) | |
// ^j | |
console.log(add7(3)) | |
thingamabob.f(1, 3) | |
// ?, 1, 3 | |
array.reduce(function (prev, next) { | |
this. | |
}.bind(this) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment