this keyword in arrow function code example
Example 1: js arrow function this
const person = {
firstName: 'Viggo',
lastName: 'Mortensen',
fullName: function () {
return `${this.firstName} ${this.lastName}`
},
shoutName: function () {
setTimeout(() => {
console.log(this);
console.log(this.fullName())
}, 3000)
}
}
Example 2: arrow function in javascript
function PrintName(name){
console.log("my name is ", name) ;
}
const PrintName = (name) => {
return console.log("my name is " , name) ;
}
Example 3: Arrow Functions
const magic = function() {
return new Date();
};
const magic = () => {
return new Date();
};
const magic = () => new Date();
Example 4: arrow function javascript
let testingFunc(string) => string == "Test" ? "Success!" : "Failure!";
console.log(testingFunc("test"));
let arrowFunc(string) => {
if (string = "test") {
return "Success!";
}
return "Failure!";
}
};
console.log(testingFunc("Test"));
Example 5: javascript this inside arrow function
function A() {
this.val = "Error";
(function() {
this.val = "Success";
})();
}
function B() {
this.val = "Error";
(() => {
this.val = "Success";
})();
}
var a = new A();
var b = new B();
a.val
b.val
Example 6: arrow function
Arrow Function is another way to write a function.
"classic method":
function x ()
{
console.log("X")
}
"arrow method":
var x= ()=>
{
console.log("X")
}
if we need to add paramiter is pritty simple:
var x = (PARAMS) =>
{
console.log(PARAMS)
}
Have a nice day