artdvp
9/7/2017 - 3:08 PM

Diving Into Javascript

CS50

by => Jordan Hayashi

Lecture 0 : Diving Into Javascript

Interpretor

Syntax

var fisrtname = "art";
var lastname = "dvp";
var arr = ['student' , 23,true , function(){
  console.log('hi')
}];

//hi 
for(var i = 0 ; i < arr.length; i++){
  console.log(arr[i]);
}

Types

  • Dynamic typing
  • Primitive tpyes(no methods)
    • undefined
  • null
  • boolean
  • number
  • string
  • (symbol)

Typecasting ? Coercion

  • Explicit vs. Implicit coercion

  • var x = 42;

  • var explicit = String(x); // explicit === "42"

  • var implicit = x + "" ; // implicit === "42"

  • == vs. ===

  • == coerces the type

  • === requires equivalent types

Coercion, cont.

  • Which values are falsy?
  • undefind
  • null
  • false
  • +0 , -0 , NaN
  • Which values are truthy?
  • {}
  • []
  • Everthing esle

Objects, Arrays ,Functions,Objects

  • ^ did i put Object twice?

  • Nope,I put it 4 time

  • Everything else is an object

  • Prototypical Inheritance

var o = new Object();
o.firstname = "art";
o.lastname = "dvp";
o.isStudent = true;
o.age = 23;
o.greeting = function() { console.log("Hey"); };

console.log(JSON.stringify(o));

var o2 = {};
o2['firstname'] = "art";
var a = 'lastname';
o2[a] = 'DVP';

var o3  = {
 firstName : "ART",
 lastName : "DvP",
 greet : function(){
   console.log('hi');
 },
 address : {
   street : 'Main st.',
   number : '111'
 }
};

Prototpical Inheritance

  • Non-primitive types have a few properties/method associated with them

  • Array.prototype.push()

  • String.prototype.toUpperCase()

  • Each object stores a reference to its prototype

  • Properties/methods defined most tightly to the instance have priority

Prototypical Inheritance

  • Most primitive types have object wrappers

  • String()

  • Number()

  • Boolean()

  • Object()

  • (Symbol())

  • JS will automatically "box" (wrap) primitive values so you have access to methods

42.toString() //Error
var x = 42;
x.toString() // "42"
x.__proto__ // [Number : 0]
x instanceof Number // false


var xx = new Number(32);
  • Why use reference
  • What's the alternative?
  • What's the danger?

Scope

  • Variable lifetime
  • Variables live from when they're declared until when their function ends
  • Hoisting
  • Function definitions are hoisted, but not varible initializations
greeting();
doesThisWork();

function doesThisWork(){
    console.log('pls');
}

function greeting(){
    console.log('Hi');
}
  • But/How why?

The Javascript Engine

  • Before executing the code, the engine reads the entire file and will throw a syntax error if one is found
  • Any function definitions will be saved in memory
  • Variable initializations will not be run ,but variable names will be declared
console.log(i);
greeting();

var i = 42;

function greeting(){
   console.log('Hi');
}

var i 

The Global Object

  • All variables and functions are actually parameters and methods on the global object
  • Browser global object is the 'window' object
  • Node.js global object is the 'global' object

Execution context

  • Equivalent to a 'stack frame' in C
  • Wrapper of varibles and functions local to a function's execution
  • Collection of execution contexts is known as the execution stack

Lexical environment

  • Determines how variable names are resolved, especially with nested functions
  • Child functions contain the scope of the parent function,even if the parent has returned
var x = 50;
function test(){
   var x = 42;
   function printX(){
       console.log(x);
   }
}

Lecture 1 : Advanced JavaSript