this
? 🧐this
The process of determining the method to invoke at run-time rather than compile-time.
“Programming using these kinds of objects does not make your code ‘object-oriented’ any more than programming with functions makes your code ‘functional’.”
—Eric Elliott
this
in JavascriptIt's about where the function was invoked, not where it was declared
var that = this
?function Animal() {
this.age = 0
setInterval(function growUp() {
this.age++
}, 1000)
}
var panda = new Animal()
setInterval(function() {
console.log(panda);
}, 1000)
function Animal() {
var that = this
that.age = 0
setInterval(function growUp() {
that.age++
}, 1000)
}
var panda = new Animal()
setInterval(function() {
console.log(panda);
}, 1000)
const animal = {
name: 'Panda',
greet() {
alert(`I am a ${ this.name }`)
},
family: {
name: 'Ursidae',
greet() {
alert(`I belong to the ${ this.name } family`)
}
}
}
animal.greet()
animal.family.greet()
function intro() {
alert(`I am a ${ this.name } and I ${l1}, ${l2} and ${l3}`)
}
const animal = {
name: 'Panda',
}
const life = ['eat', 'play', 'sleep']
intro.call(animal, life[0], life[1], life[2])
function intro() {
alert(`I am a ${ this.name } and I ${l1}, ${l2} and ${l3}`)
}
const animal = {
name: 'Panda',
}
const life = ['eat', 'play', 'sleep']
intro.apply(animal, life)
new
bindingfunction Animal (name) {
this.name = name
}
const panda = new Animal('Panda')
function Animal() {
this.age = 0
setInterval(() => {
this.age++
}, 1000)
}
const panda = new Animal()
setInterval(function() {
console.log(panda);
}, 1000)
function whatIsThis() {
return this;
}
// In a browser:
whatIsThis() === window; // true
// In Node:
whatIsThis() === global; // true
function whatIsStrictlyThis() {
'use strict';
return this;
}
whatIsStrictlyThis() === undefined; // true
function Animal() {
this.age = 0
var boundGrowUp = (function growUp() {
this.age++
}).bind(this)
setInterval(boundGrowUp, 1000)
}
var panda = new Animal()
setInterval(function() {
console.log(panda);
}, 1000)
Let's talk about event handling…
// JSX passes event handler as function
<button onClick={handleClick}> Click me</button>
// HTML passes inline event handler as string
<button onclick="handleClick()">Click me</button>
this
works rightclass Button extends React.Component {
constructor(props) {
super(props);
this.state = {
clicked: false
};
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState(state => ({
clicked: !state.clicked
}));
}
render() {
return (
<button onClick={this.handleClick} />
);
}
}
class Button extends React.Component {
state = { clicked: false };
handleClick = () => {
this.setState(state => ({
clicked: !state.clicked
}));
};
render() {
return (
<button onClick={this.handleClick} />
);
}
}
function Button() {
const [clicked, setClick] = useState(false);
const handleClick = () => setClick(!clicked);
return (
<button onClick={handleClick} />
);
}
“To some zoologists, all animals are pandas, just vary by color…”
–My wise friend, Gao Wei
Hui Jing does more bo liao tings
Github Repo: https://github.com/huijing/sgtechonline