Skip to content

6 月 2018

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

Type in JavaScript

primitive value
  • undefined
  • null
  • boolean
  • number (only one number type…)
  • string
  • symbol (ES6)
coercion

expected

weird parts

loose equal vs strict equal

use === to prevent coercion and do not use == in 99% cases except you mean to coerce

ref:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness

Python Name Conventions

Must start with (_) or letter (a-z, A-Z), follow by any number of (_) or letter(a-z, A-z) or digit (0-9) except reserved words

Conventions

_my_var: indicate “internal use” or “private” object, cannot get imported by

__my_var: used to mangle class attributes, useful in inheritance chain

__my_var__: system defined

PEP8 style guide

Python Multi-line Statements

How python interpret multi-line code into single line code:
  1. python program
  2. physical lines of code(end with a physical newline CHARACTER create by enter)
  3. logical lines of code(end with a logical NEWLINE token)
  4. tokenized
  5. execute
physical newlines vs logical newline

sometimes physical newlines are ignored in order to combine multiple physical lines into a  single logical newline

break implicitly: [], (), {}

break explicitly

multi-line strings

multi-line strings are regular string, not comments (can be used as docstring)

escaped characters(\n, \t), non-visible characters(newlines, tabs) in multi-line are part of string; escaped characters will formatted when print it

ref:

https://github.com/fbaptiste/python-deepdive/blob/master/Part%201/Section%2002%20-%20A%20Quick%20Refresher/01%20-%20Multi-Line%20Statements%20and%20Strings.ipynb

Python Type Hierarchy

Number

  • Integral: Integer, Booleans
  • Non-Integral: Floats, Complex, Decimals, Fractions(1/3)

Collection

  • Sequences
    • Mutable: List
    • Immutable: Tuples, Strings
  • Sets
    • Mutable: Sets
    • Immutable: Frozen Sets
  • Mappings
    • Dictionaries (relate to set)

Callables

  • Built-in Functions
  • User-Defined Fuctions
  • Instance Methods (e.g. len())
  • Built-in Method (e.g. my_list.append(x))
  • Generators
  • Classes
  • Class Instances(__call__())

Singletons

  • None
  • NotImplemented
  • Elipsis operators

Event Queue

When the browser, somewhere outside the JavaScript engine, has the event inside the JavaScript engine that want to be notified of, its get placed in the event queue, and WHETHER OR NOT the event have a function response to it.

The event queue gets looked at by JavaScript when the execution stack is EMPTY.

JavaScript engine is always running synchronously, what happening is that the browser is asynchronously putting things into event queue.