0%

Java & JS Commonness

Code Structure

  • expr, stmt ; .. & blocks {..} ..
    • JS line break & auto-semicolon insertion (no implicit “;” af incomplete expr & bf [] () ..) ..
  • comment: inline / mtp-line .., javadoc/JSDoc ..
  • Coding style & linters ..

Variable

A named storage in memory for data

  • <type>: allowed value, ops & memory used, e.g. primitive / reference type
    • static/dynamic typing: compile/runtime type checking
  • <name>: descriptive ref of value (no keyword, UPPERCASE_CONST)
  • <value>: literal / reference
    • null literal: obj unavailable marker (instanceof => false)
  • var scope & var shadow

Char

Operator

  • comparison & equality check ops => bool
  • conditional op ?..:, logical ops ! && ||
  • bitwise/bit shift ops .., op precedence & op return

Control flow

  • if..else, switch (block falls througth 贯穿 ==> case group)
  • while & do..while, for & for..of (JS) & for..: (Java)
  • break/continue (non-expr in JS, not in ?..) & label

Function

Fn basics ..

  • fn naming …
  • fn-param: declare in fn(..) & init when fn-call by args
    • pass args to fn: copy value to fn-params (use as a local var in fn-body, change not seen outside, default value)
    • pass method to fn: callback fn, lambda expr
    • rest param: fn(.., ...arr) & method(.., int... arr)
  • local var “shadow” outer var, global var
  • return: default return undefined / null , (...) for mtp-line value

Array

  • arr.length, arr[i]
  • 186****5678: fill(..)

String

  • immutable, concat op +
  • get info: charAt(i), indexOf(), starts/endsWith(), contains();
  • modify: replace(), split(), substr(), rm space:trim(), replaceAll("\\s", "")

Obj-oriented

  • OOP 3大基本特性: 封装,继承,多态 (一种定义,多种实现)

Class

  • class & obj: prototype & instance

  • class member: field (value of state) & method (implementation of behavior), nested type;

  • constructor & new op, obj.<member>; this(..) & this.<member>;

  • static method: no access to instance members, no this

  • Enum: a fixed set of consts

Inheritance

  • extends, super; method/constructor & fields override
  • call super(..) in child.con():
    • JS uses parent.con() to create & init this for child instance when new ..
    • In JS, call parent.con() in child class, parent.con uses parent field value, not the overridden one in child class ..
  • super() called implicitly (Object has a empty con()), call super(..) explicitly [p159]
  • instanceof op

GC: manually drop ref by null

Refs