Skip to content

Latest commit

 

History

History
208 lines (200 loc) · 4.54 KB

02.javascript_general.md

File metadata and controls

208 lines (200 loc) · 4.54 KB

javascript types

  • Number
  • String
  • Boolean
  • Symbol (new in ES2015)
  • Object
    • Function
    • Array
    • Date
    • RegExp
    • Math
  • null
  • undefined

Number

  1. Math, a build-in Object.
Math.sin(3.5);
var circumference = 2 * Math.PI * r;
  1. parseInt(), a build-in Function.
parseInt('123', 10); // 123
parseInt('010', 10); // 10
/* convert a binary number to an integer */
parseInt('11', 2); // 3
  1. parseFloat(), a build-in Function, it always use base 10.
  2. NaN, is a special value (short for "Not a Number")
parseInt('hello', 10); // NaN
/* NaN is toxic */
NaN + 5; // NaN
  1. isNaN(), a build-in Function.
isNaN(NaN); // true
  1. Infinity and -Infinity, are special value.
1 / 0; // Infinity
-1 / 0; // -Infinity
  1. isFinite(), a build-in function
isFinite(1 / 0); // false
isFinite(-Infinity); // false
isFinite(NaN); // false

String

  1. length of string.
'hello'.length; // 5
  1. String hava method.
'hello'.charAt(0); // "h"
'hello, world'.replace('hello', 'goodby'); // "goodby, world"
'hello'.toUpperCase(); // "HELLO"
'hello'.toLowerCase(); // "hello"

Other types

  1. null and undefined are difference
  2. false, 0, empty, String(""), NaN, null, undefined all become false. All other values become true.
Boolean(''); // false
Boolean(234); // true
  1. &&, ||, ! operator for boolean value.

Variables

  1. define variable use one of three keywords: let, const, var
  2. let define block level variable, variable is available from the function block it is enclosed in.
let a;
let name = 'Simon';

scope of let variable

// myLetVar *not* visible here
for (let myLetVar = 0; myLetVar < 5; myLetVar++) {
  //myLetVar is only visible in here
}
// myLetVar *not* visible here
  1. const , declare const var which never intend to change
const PI = 3.14; 
PI = 1; // will throw an error since can't change const var value
  1. An important difference between JavaScript and other languages like Java is that in JavaScript, blocks do not have scope; only functions have scope. So if a variable is defined using var in a compound statement (for example inside an if control structure), it will be visible to the entire function. However, starting with ECMAScript 2015, let and const declarations allow you to create block-scoped variables.

Operators

  1. +, -, *, /, %
  2. +=, -=, ++, --
x += 5;
x = x + 5;
  1. + , also does string concatenation
'hello' + 'world'; // "hello world"

add string to number, everything converted to a string first.

'3' + 4 + 5; // "345"
3 + 4 + '5'; // "75"
  1. Compare, <, >, <=, >=
  2. == , perform type coercion.
123 == '123'; // true
1 == true; // true
  1. ===, avoid type coercion.
123 === '123'; // false
1 === true; // false

Control structures

  1. if, else if, else control
if () {
  //
} else if () {
  // 
} else {
  //
}
  1. while, do while loop
while (true) {
  // an infinite loop
}
// �do while loop
var input;
do {
  input = get_input();
} while (isValid(input));
  1. for loop.
for (var i=0; i < 5; i++) {
  // traditional for loop control
}
// for ... of , Array
for (let value of array) {
  // for of Array loop 
}
// for ... in , property in Object loop
for (let property in object) {
  // for in, property in object loop
}
  1. && and || 是短路逻辑 (short-circuit),即第二个值依赖第一个结果。
var name = o && o.getName();
var name = cachedName || (cachedName = getName());
  1. a ? 'yes': 'no' operator
var allowed = (age > 18) ? 'yes' : 'no';
  1. switch case, use for multi branch base on number or string
switch (action) {
  case 'draw':
    drawIt();
    break;
  case 'eat':
    eatIt();
    break;
  default:
    doNothing();
}

no break statement, execute will "fall through" to next level, it's better add comments to "fall through" if you really want it.

  switch (a) {
    case 1: // fall through
    case 2:
      eatIt();
      break;
    default:
      doNothing();
  }

Object

  1. javascript Object can be thought of as simple collections of name-value pairs, which similar to:
  • Dictionaries in Python
  • Hashes in Perl and Ruby
  • Hash table in C and C++
  • HashMap in Java
  • Associative arrays in PHP
  1. Everything in javascript is object. javascript program involves a great deal of hash table look up. This good they are so fast!
  2. two type create empty object:
// type 1
var obj = new Object();
// type 2
var obj = {};