function handler() {
var val = this.value;
// ...
}
var person = {
name: "John",
sayName: function() {
console.log(this.name);
}
};
person.sayName(); // "John"
var person = {
sayName: function() {
console.log(this.name);
},
name: "John"
};
var ceo = { name: "Mark" };
ceo.sayName = person.sayName;
person.sayName(); // "John"
ceo.sayName(); // "Mark"
function independent() {
console.log(this.name);
}
var person = {
name: "John"
};
person.sayName = independent;
person.sayName(); // "John"
var nameCaller = {
sayName: function() {
console.log(this.name);
}
};
var person = Object.create(nameCaller);
person.name = "John";
person.sayName(); // "John"
function fullName() {
return this.firstName + " " + this.lastName;
}
var person = { firstName: "John", lastName: "Doe" };
Object.defineProperty(person, "name", { get: fullName });
console.log(person.name); // "John Doe"
function func() {
console.log(this);
}
func(); // ?
var person = {
sayName: function() {
console.log(this.firstName); // ?
console.log(this); // ?
},
firstName: "John"
};
var func = person.sayName;
func();
var person = {
firstName: "John",
sayName: function() {
setTimeout(function() {
console.log(this.firstName); // ?
console.log(this); // ?
}, 1000);
}
};
person.sayName();
<input
type="button"
onclick="this.value='changed';"
value="click me" />
<input type="button" onclick="handler()" />
function handler() {
console.log(this); // ?
}
<input type="button" onclick="handler(this)" />
function handler(element) {
console.log(this); // ?
console.log(element); // ?
}
<input type="button" onclick="handler" />
function handler() {
console.log(this); // ?
}
This is not a valid way to specify on* attributes
<input type="button" id="submit" />
function handler() {
console.log(this); // ?
}
var element = document.getElementById("submit");
element.onclick = handler;
<input type="button" id="submit" />
function handler() {
console.log(this); // ?
}
var element = document.getElementById("submit");
element.addEventListener("click", handler);
$("p").click(function() {
this.style.backgroundColor = "yellow";
$(this).css("color", "blue");
});
$("a").text(function() {
return this.getAttribute("href");
});
$("a").each(function() {
console.log(this.getAttribute("href"));
});
var arr = [ "aa", "bb", "cc", "dd" ];
$.each(arr, function() {
console.log(this);
});
function Person() {
this.name = "John";
}
var p = new Person();
console.log(p.name); // John
function Person() {
this.name = "John";
return { name: "Mark", age: 20 }
}
var p = new Person();
console.log(p.name); // ?
function Person() {
this.name = "John";
return 50; // :-o
}
var p = new Person();
console.log(p.name); // ?
var obj = {
name: "John",
outer: function() {
console.log("Outer: " + this.name); // ?
function inner() {
console.log("Inner: " + this); // ?
}
inner();
}
}
obj.outer();
var obj = {
name: "John",
func: function() {
eval ("console.log(this)"); // ?
(1, eval) ("console.log(this)"); // ?
}
}
obj.func();
var arr = [ "11", "22", "33", "44"];
arr.forEach(function() {
console.log(this); // ?
});
arr.forEach(function() {
console.log(this); // ?
}, { name: "John" });
var person = {
sayName: function() {
console.log(this.firstName); // <==
},
firstName: "John"
};
var func = person.sayName;
func()
person.sayName()
var person = {
firstName: "John",
sayName: function() {
setTimeout(function() {
console.log(this.firstName); // undefined
}, 1000);
}
};
person.sayName();
var person = {
firstName: "John",
sayName: function() {
var self = this; // <==
setTimeout(function() {
console.log(self.firstName); // ?
}, 1000);
}
};
person.sayName();
var obj = {
age: 20,
outer: function() {
function inner() {
console.log(this.age); // undefined
}
inner();
}
}
obj.outer();
var obj = {
age: 20,
outer: function() {
var that = this; // <==
function inner() {
console.log(that.age); // ?
}
inner();
}
}
obj.outer();
func.call(thisArg, arg1, arg2, arg3, ...)
var person = {
sayName: function() {
console.log(this.name);
}
};
var ceo = { name: "Mark" };
person.sayName.call(ceo); // ?
var nodeList = document.getElementsByTagName("h2");
var firstTwo = nodeList.slice(0, 2);
var firstTwo = Array.prototype.slice.call(nodeList, 0, 2);
function Person(name) {
this.name = name;
}
Person.prototype.method = function() { /* ... */ }
function Employee(name, department) {
Person.call(this, name); // <==
this.department = department;
}
Employee.prototype = new Person;
Employee.prototype.constructor = Employee;
var e = new Employee("John Doe", "Marketing");
console.log(e.name, e.department); // ?
func.apply(thisArg, argsArray)
var max = Math.max(82, 12, 1, 52, 27);
console.log(max);
var numbers = [ 82, 12, 1, 52, 27 ];
var max = Math.max.apply(null, numbers);
console.log(max);
function maxInArray(arr) {
var max = -Infinity;
for(var i = 0; i < arr.length; i++)
if(arr[i] > max) max = arr[i];
return max;
}
var numbers = [ 82, 12, 1, 52, 27 ];
console.log(maxInArray(numbers));
function maxInArray(arr) {
return Math.max.apply(null, arr);
}
var numbers = [ 82, 12, 1, 52, 27 ];
console.log(maxInArray(numbers));
func.bind(thisArg, arg1, arg2, arg3, ...)
var person = {
sayName: function() {
console.log(this.firstName); // ?
},
firstName: "John"
};
var func = person.sayName.bind(person); // <==
func();
var person = {
firstName: "John",
sayName: function() {
setTimeout(function() {
console.log(this.firstName); // ?
}.bind(this), 1000); // <==
}
};
person.sayName();
var log = console.log;
log("Hello!"); // ?
var log = console.log.bind(console);
log("Hello!"); // ?
<span onclick="alert(this)">Click me</span>
<a href="http://google.com/" onclick="alert(this)">Google</a>
Click me
var obj = {
name: "John",
func: function() {
eval.call(this, "console.log(this)"); // ?
}
}
obj.func();