JavaScript Flashcards
What is ECMA?
ECMA = an organisation that defines standards for technologies
What is ECMAScript?
A specification developed by ECMA
JavaScript is a programming language that conforms to the ECMAScript specification
What are classes?
Classes are syntactic sugar over prototypes and prototypical inheritance.
Because JavaScript is dynamically typed, classes in JS differ to those in say C#.
What are Value (Primitive) Types?
Number String Boolean Undefined Null Symbol
Primitives are copied by their VALUE.
What are Reference Types?
All reference types are essentially OBJECTS!
e.g. Functions & Arrays
Objects are copied by their REFERENCE
PRIMITIVE TYPES:
let x = 10; let y = x;
x = 20;
What is the value of y?
Primitives are copied by their VALUE.
x = 20; y = 10;
These variables are independent of each other.
With primitive types, the value 10 is stored inside x: let x = 10
When we copy that var, the value that is stored inside it is copied into the new var let x = 10; let y = x; // y = 10;
x = 20;
This is not the case when we use reference types (objects)
REFERENCE TYPES:
let x = { value: 10 }; let y = x;
x.value = 20;
What is the value of y?
Objects are copied by their REFERENCE
let x = { value: 10 }; let y = x;
x.value = 20; y = 20;
When an object is used, it does not get stored inside ‘x’ itself.
It gets stored somewhere else in memory & the address of that location in memory is what gets stored in the variable ‘x’.
e.g. let x = { value: 10 };
object gets stored in memory
1233 = {value:10}
SO:
x = 1234 (1234 holds a reference to that place in memory which holds the object)
therefore when assigning ‘y’ the value of ‘x’, we actually assign it the reference in memory
What is the difference between scope & closure in JavaScript?
In scope, the variables are temporary.
E.G.
- a function within a function
- child function has two variables x & y
- variables are re-created & re-initalised each time the child function is called
- after the function has completed, the variables in the child function die in the scope
In closure, the variables within the parent function stay initialised
What is classical inheritance?
we have prototypical inheritance in JS, NOT classical but it’s good to know the difference
Instead of redefining common methods, they can be defined and then inherited by other classes.
It allows us to re-use code easily & eliminate redundant code.
Reduces opportunity for bugs (no spaghetti code!).
Makes it easier to change/maintain code as you just need to look in one place to make adjustments.
E.G.
base/super/parent class
= Shape
has a function = startLocation()
derived/sub/child class
= Circle, Square, Triangle
inheritance relationship = IS-A
E.G. Circle IS-A Shape, and inherits it’s function
What is a prototype?
Every object in JS, except ___ has a prototype (parent).
This object inherits everything defined in the prototype.
Think of it as a starting base set up for all objects.
What are Falsy values?
undefined null 0 false '' NaN
What are Truthy values?
Anything that returns true from a boolean statement/is not falsy
What is coercion?
Implicit & Explicit
EXPLICIT: var a = "42" //string var b = Number(a); // a becomes a number
IMPLICIT: var a = "42" //string var b = a * 1 ; // 42 becomes the number because we perform a math operation on it.
What is scope?
Availability of variables and functions within local, global or lexical scope?
What are the two different types of equality?
Strict comparison === checks for value & type equality (no coercion)
Abstract comparison == checks for value equality only (with coercion allowed)
E.G: var a = "42"; var b = 42;
a == b; // true
a === b; // false