Javascript basics

A

2 min read    12 May 2017    

Javascript basics by Arjun

JS interpreted lang. js - langauge for the web, browser

chrome - v8, edge - chakra : js spec

context based, startTime

-----------------------
|                     |
|         ____        |
|       _|_g_|_       |
|    ___|__f___|_     |
|   |     TOP    |    |
-----------------------

Call stack like SPIM.

infilop - max call stack limit - blew it.

xhr - xml http request, not related to xml

setTimeOut => calls webAPI => Schedule in event loop | only once

setInterval => repeated

Javascript is originally called Ecmascript. es5 and es6 are just different versions of the spec. Every new version is a superset of previous versions. es6 [latest version] should be able to work with all browsers. Javascript’s first version came out in 1994, which was supposedly built in 10 days, so practically a student project. :sweat_smile:.

Consider es5. Scoping works in js differently, since there are no types.only var

"5" + "1" = "51"
"4" - 1 = "3"

es5 - String, number, boolean, objects, functions, undefined are the 6 types.

null vs undefined - null is a value, undefined is an object. Null is assigned to a var.

null == undefined => True
null === undefined => False [Type is different]

Fact - !!var - used for checking, but why this ? - converts to Boolean

Hoisting - hoisting a flag : take last defined value since it is interpreted. Functions only hoisted if its value.

Functions are values - can be assigned to variable, function can return another function. stackTrace is unwinding your call stack.

Within the same scope, let will not allow me to create a var with already existing name. let is block scope, cannot redeclare, can edit. const is fixd, cannot reassign. var defines in function scope

Execution scope:

  • Global scope
    • Function scope
    • Block scope

Syntactic Sugar

operate on functions possible. If calculator has sep functions, add and sub, this is composition not inheritance. Functions are values, with constraints.

mutable - Strings are immutable.

obj.name

obj[0] - Array is an object with keys as strings or numbers

how are this different ? . only if valid JS var name. No spaces allowed.

obj [a+b] => a+b is evaluated p[2+3] is p[5], not p[‘2+3’]

Polymorphism not allowed.

C(a,b) and C(a, b, d) are defined, but C(p, q) is called - last value is called.

E(a, b) is called with E(a): b init to undefined. E(a, b, c) - c will be unused, no error is returned.

‘Method’ vs ‘function’ -

Youtube - What the hell is an event loop anyway.

  • inheritance
~ > node                                     Friday 12 May 2017 12:37:36 PM IST
> let empty = {}
undefined
> empty = toString()
'[object Undefined]'
> empty.toString()
'[object Undefined]'
> empty.toString
[Function: toString]
> empty.a
undefined
> Object.getPrototypeOf(empty)
{}

JS is camelCase. - If var starts with Capital, it is function

No class in js, only prototypes.

Object is assoc with a proto,

prototypes are just objects.

clojure - functions in es5 are the only ones to create new scopes.

Read about this in this long weekend.

function multiplier (n) {
  let locvar = n;

  return function(number) {
    return locvar * number;
  }
}

...
let twice = multiplier(2); // THIS is a function now
twice(5);

clojure is just a way to access inner scope vars.

Some rights reserved.

Leave a Comment