Function and Class

Function adalah object

Special Object di JavaScript

First Class-Citizen

function diperlakukan seperti tipe data lainnya

or First Class Function 

  • bisa dimasukkan sebagai parameter atau arguments
  • bisa di set sebagai nilai ke dalam variable (function expression)
  • bisa dikembalikan dari function lain (function return a function)
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 and function 

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()

By Value and By Reference

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)

Hoisting

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

`arguments` keyword

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);

IIFEs

(function() {
    console.log("I'am Iffy");
})();

Closure

Callback

Constructor function and class

delegate

Propotype

`new`  keyword

apply, bind and call

Higher order Function

Functions and Class

By ikhsanalatsary

Functions and Class

  • 73