Skip to content

Instantly share code, notes, and snippets.

@liqiang372
Last active February 9, 2017 23:27
Show Gist options
  • Save liqiang372/2e69338e5b025211e00d6267944e9a63 to your computer and use it in GitHub Desktop.
Save liqiang372/2e69338e5b025211e00d6267944e9a63 to your computer and use it in GitHub Desktop.
for enhui study
var x = 1;
console.log("whatever")
// The "this" keyword in JavaScript
window.name = "shenmegui";
var fruits = {
name: "fruits",
color: "nocolor",
eat: function() {
console.log("eating " + this.name);
}
}
fruits.eat(); // eating fruits;
var x = fruits.eat;
x(); // eating shenmegui
x.bind(fruits)(); // eating fruits;
// call and apply
function addAndSetX(a, b) {
this.x += a + b;
}
var obj = {x : 1};
addAndSetX.call(obj1, 1, 1); // this = obj1, obj = { x : 3}
addAndSetX.apply(obj1, [1, 2]); // this = obj1, obj = { x : 6}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment