Object methods, "this"
Method examples
// funciton expression
let user = {
name: "John",
age: 30
};
user.sayHi = function() {
alert("Hello!");
};
user.sayHi(); // Hello!
// same function declaration
let user = {
// ...
};
// first, declare
function sayHi() {
alert("Hello!");
};
// then add as a method
user.sayHi = sayHi;
user.sayHi(); // Hello!Method shorthand
“this” in methods
“this” is not bound
Calling without an object: this == undefined
this == undefined Internals: Reference Type
Arrow functions have no “this”
Last updated