// Regular Function With Map
const upperizedNames = ["Dimas", "Widy", "Buchori"]
.map(function(name) {
return name.toUpperCase();
})
console.log(...upperizedNames);
/* output:
DIMAS WIDY BUCHORI
*/
// Arrow Function With Map
const upperizedNames = ["Dimas", "Widy", "Buchori"]
.map(name => name.toUpperCase())
console.log(...upperizedNames);
/* output:
DIMAS WIDY BUCHORI
*/
// function arrow expression
const sayHello = greet => console.log(`${greet}!`);
const sayName = name => console.log(`Nama saya ${name}`);
console.log(sayHello("Ubaidillah"))
// const sayName = name => console.log(`Nama saya ${name}`);
const coba = _ => console.log(`test with no parameter _`)
coba()
// Arrow Function Store in Argumen
["Dimas", "Widdy", "Buchori"].forEach(name => console.log(`Nama saya ${name}`));
// Arrow Function Store in Property
const user = {
introduce: name => console.log(`Nama saya ${name}`)
}
user.introduce("ubaidillah")
//=========================================================================//
// Arrow Function Multi Parameter
const sayHello = (name, greet) => console.log(`${greet}, ${name}!`);
sayHello("Dimas", "Selamat Pagi")
/* output:
Selamat Pagi, Dimas!
*/
//=========================================================================//
// Arrow Function Without Parameter
const sayHello2 = () => console.log("Selamat pagi semuanya!");
sayHello()
/* output:
Selamat pagi semuanya!
*/
//=========================================================================//
// Arrow Function Body Block
const sayHello3 = language => {
if(language.toUpperCase() === "INDONESIA") {
return "Selamat Pagi!";
} else {
return "Good Morning!";
}
}
console.log(sayHello("Indonesia"));
/* output:
Selamat Pagi!
*/
//=========================================================================//
//Regular Function this Keyword
function People(name, age, hobby) {
this.name = name;
this.age = age;
this.hobby = hobby;
}
// menambahkan introMyself ke People
People.prototype.introMyself = function () {
// this -> People
setTimeout(function() {
// this -> ??
console.log(`Hello! Nama saya ${this.name}, umur saya ${this.age}.`)
console.log(`Hobby saya adalah ${this.hobby}`)
}, 300)
}
const programmer = new People("John", 18, ["Coding", "Read book", "Ping-pong"]);
programmer.introMyself();
/* output:
Hello! Nama saya undefined, umur saya undefined.
Hobby saya adalah undefined
*/
//=========================================================================//
//Arrow Function this Keyword
function People(name, age, hobby) {
this.name = name;
this.age = age;
this.hobby = hobby;
}
// menambahkan introMyself ke People
People.prototype.introMyself = function () {
// this -> People
setTimeout(() => {
// this -> People
console.log(`Hello! Nama saya ${this.name}, umur saya ${this.age}.`)
console.log(`Hobby saya adalah ${this.hobby}`)
}, 300)
}
const programmer2 = new People("John", 18, ["Coding", "Read book", "Ping-pong"]);
programmer2.introMyself();
/* output:
Hello! Nama saya John, umur saya 18.
Hobby saya adalah Coding,Read book,Ping-pong
*/