邹明潮 Notes: YDKJS UP & GOING

邹明潮
KevinZou
Published in
3 min readMar 22, 2017

Executing a Program

  1. Compiled Language like C++ and Java. First complies the program into machine-language instructions, then executes the complied instructions.
  2. Interpreted Language such as Python. Runs the program directly without compiling.

The Javascript engine actually compiles the program on the fly and then immediately runs the program.

Dive into Javascript

Values & Types

Only values have types in Javascript, variables are just simple containers for those values.

Objects

Bracket notation : it requires either a variable or a string literal

  • Special characters in a property name like obj[“Kevin”].
  • Get access to a property/key but the name is stored in another variable.
var obj = {
a: "hello world",
b: 42
};

var b = "a";

obj[b]; // "hello world"
obj["b"]; // 42

Comparing Values

1. Falsy values in Javascript are the followings:

  • “”
  • 0, -0, NaN
  • null, undefined
  • false

2. Equality: ‘===’ checks for both value and type equality

  • If either value in a comparison could be the true or false value, avoid == and use ===.
  • If either value in a comparison could be of these specific values (0, "", or [] -- empty array), avoid == and use ===.
  • In all other cases, you’re safe to use ==. Not only is it safe, but in many cases it simplifies your code in a way that improves readability.

Function

1. Hoisting: when declarations are conceptually “moved” to the tops of its enclosing scope.

2. In ES5, only a function has a scope and is considered as an expression, much like any other value or expression.

3. Immediately Invoked Function Expressions(IIFES)

var x = (function IIFE(){
return 42;
})();

x; // 42

4. Closure is a way to “remember” and continue to access a function’s scope(its variable) even once the function has finished running.

function makeAdder(x) {
// parameter `x` is an inner variable

// inner function `add()` uses `x`, so
// it has a "closure" over it
function add(y) {
return y + x;
};

return add;
}

var plusOne = makeAdder( 1 );
var plusTen = makeAdder( 10 );

plusOne( 41 ); // 42 <-- 1 + 41
plusTen( 13 ); // 23 <-- 10 + 13

5. Modules: let you define private implementation details that are hidden from the outside world, acting as a public API.

This Identifier

This reference usually points to an object. But which object it points to depends on how the function was called(call-site). And this does not refer to the function itself.

Prototype

The internal prototype reference linkage from one object to its fall-back happens at the time the object is created.

Polyfilling

Producing a piece of code with a newer feature is able to run in older JS environments.

// ES6 defines a utility called Number.isNaN(..)
if (!Number.isNaN) { //polyfill the utility for the older browser
Number.isNaN = function isNaN(x) {
return x !== x;
};
}

Transpiling

Converting your newer code into older code equivalents by employing a tool like Babel, when there is no way to polyfill new syntax.

Non-Javascript

  • DOM API: this interface exists in a browser, which traditionally implemented by C/C++.
  • Input/Output: alert(…) is provided to your JS program by the browser, not by the JS engine itself. The call you make sends the message to the browser internals and it handles drawing and displaying the message box. The same goes with console.log(…).

--

--