function diperlakukan seperti tipe data lainnya
const foo = function() {
console.log("foobar");
}
// Invoke it using the variable
foo();
function sayHello() {
return "Hello, ";
}
function greeting(helloMessage, name) {
console.log(helloMessage() + name);
}
// Pass `sayHello` as an argument to `greeting` function
greeting(sayHello, "JavaScript!");
function sayHello() {
return function() {
console.log("Hello!");
}
}
const sayHello = function() {
return function() {
console.log("Hello!");
}
}
const myFunc = sayHello();
myFunc();
function sayHello() {
return function() {
console.log("Hello!");
}
}
sayHello()();Object
0x001
Primitive
"Property"
Function
"Property"
Object
"Method"
0x002
0x003
0x004
const person = {
firstName: 'Abdul Fattah',
lastName: 'Ikhsan',
displayName: function() {
return this.firstName + ' ' + this.lastName
}
}
// akses object
var lastNameProp = "lastName"
person["firstName"]
person[lastNameProp]
person.dispayName()Variable a
Primitive
Value
Copy of Primitive
Value from a
Variable b
b = a
sekalipun didalam function
0x001
0x002
By Value
make a copy
Variable a
Object
Value
Variable b
b = a
sekalipun didalam function
0x001
by reference
(sharing memory)
Global
Object
this
Outer Environment
Set up memory Space for
variables and functions
("Hoisting")
Execution Context is created (Creation phase)
Variable Environment
this
Outer Environment
Execution Context is created (Function phase)
arguments
Spesial kata kunci yang ada di dalam function, dia seperti object Array yang berisi parameter-parameter yang di masukkan ke dalam function
function func1(a, b, c) {
console.log(arguments[0]);
// expected output: 1
console.log(arguments[1]);
// expected output: 2
console.log(arguments[2]);
// expected output: 3
}
func1(1, 2, 3);(function() {
console.log("I'am Iffy");
})();