Skip to content

Function in JavaScript

JavaScript functions are objects that has other special properties:

  • Name: optional, can be anonymous
  • Code: invokable, you can RUN these code by ()
Function statement

Create a object with NAME attribute and point to memory in the creation of execution context

Function expression (anonymous)

Create a object without NAME(already have a variable knows where this object lives) attribute and point to memory in the creation of execution context, and assign this object to function when this line of code is execute

Immediately Invoked Function Expressions (IIFE)

Create the function object on the fly and invoke it immediately, and assign what it return to the variable.

Why use iife and why it is safe

Code wrapped in IIFE does not interfere with, crash into, or be interfered by any other code included in the application.

Methods

In one function’s execution context, we have 1. Variable Environment 2. Outer Environment 3. “this”.

Q: How to control “this” variable?

JavaScript functions also has these methods(can control “this”):

  • call()
  • apply()
  • bind()

Functional Programming

We should think and code in terms of functions

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *