22
loading...
This website collects cookies to deliver better user experience
// function declaration
function double(n) {
return n * n
}
// function expression
const double = function (n) {
return n * n
}
function
keyword. Instead, they have the fat arrow syntax.// arrow function
const double = n => n * n
function globalFunc() {
console.log(this)
}
globalFunc() // Window
const obj = {
meth: function () {
console.log(this)
},
}
obj.meth() // Object
const obj = {
meth: function () {
console.log(this)
function hello() {
console.log(this)
}
hello() // Window
},
}
obj.meth() // Object
hello
that is invoked inside a method
. However, since the function hello
was invoked normally, it ends up in global context. This becomes an issue when we have to use variables defined inside the object.const obj = {
name: "Mike",
meth: function () {
console.log(this)
function hello() {
console.log("My name is " + this.name)
}
hello() // My name is undefined
},
}
obj.meth() // Object
hello
has global scope, this
represents Window
object. So the value of name
is undefined.bind()
(or arrow functions, which will discuss in a while).const obj = {
name: "Mike",
meth: function () {
console.log(this)
function hello() {
console.log("My name is " + this.name)
}
const bindedHello = hello.bind(this)
bindedHello() // My name is Mike
},
}
obj.meth() // Object
bind()
method is used to bind a function to a scope. In the above case, we binded function hello
to scope of obj
which is the value of this
. Now the bindedHello()
is being executed inside the obj
scope. So variable name
has value Mike.this
. There will also be a swarm of confusion on which method is appropriate - bind, call, or apply. There is also a reduced performance associated with the bind()
method.hello
had lexical scoping, it would have object scope instead of window scope. So, the big deal about arrow functions is that it comes with lexical scoping.const obj = {
name: "Mike",
meth: function () {
console.log(this)
const hello = () => {
console.log("My name is " + this.name)
}
hello() // My name is Mike
},
}
obj.meth() // Object
this
depending on how they're invoked. It inherits scope of its parent. In the above case, function hello
inherited scope from meth
. Since meth
has object scope as it was invoked as a method, so will function hello
.const obj = {
meth: () => {
console.log(this)
},
}
obj.meth() // Window
this
, it doesn't matter whether it was invoked as a method in the above case. It will always inherit the scope of parent. The parent here is obj
which is in window scope.this
or super
(due to lexical scoping)bind
, call
, and apply
methods doesn't work as intended as they cannot establish a new scopenew
keyword to create new instances. It is not possible to create a new instance of an arrow function when you cannot actually change the scope.// regular function
function double(n) {
return n * n
}
// arrow function
const double = n => n * n
const numbers = [1, 2, 3, 4, 5]
const doubled = numbers.map(n => n * 2)
console.log(doubled) // [2, 4, 6, 9, 10]