JS is a lightweight interpreted (or just in time compiled) programming language with first-class functions. While it is most well-known as the scripting langugae for Web pages, many non-browser environments also use it, such as Node.js. JS is prototype-based, multi-paradigm, single-threaded, dynamic language, supporting object-oriented, imperative, and declarative (e.g. functional programming) styles.
- data types and variables, let and const
- looping: for, for…of, map, filter;
- functions: builtin, custom, anonymous (no name), arrow functions (lambdas)
VAR vs LET vs CONST
var:
function scoped
undefined when accessing a variable before it’s declared
let:
block scoped
ReferenceError when accessing a variable before it’s declared
const:
block scoped
ReferenceError when accessing a variable before it’s declared
can’t be reassigned
Note; Unlike var and function, when let and const and class are used in the global scope, they do not create global properties.
Functions:
Named functions are traditional declarations:
function name(params)
{
return var
}Anonymous function lacks name:
result = function(params)
{
return var
}(args)Arrow functions are a concise way to write functions:
(params) => {
return var
}If the function body is a single expression, you can omit the braces and the return keyword:
params => varFirst class functions mean that functions can be treated as objects, allowing them to be passed as arguments to other functions, assigned to variables, or returned from functions. Once we have anonymous functions or nested functions, it becomes natural for them to refer to variables outside of their body.
function func_that_takes_no_argument() {
...
}
function func_that_takes_argument(arg) {
...
}
function func_out(callback) {
callback()
}
func_out(function_that_takes_no_argument);
const arg = "example"
func_out(() => func_that_takes_argument(arg));JS objects:
Python and Javascript differ fundamentally on objects. Python has class based inheritance and Javascript has prototype based inheritance. In Javascript, objects are the same as the dictionary data structure, so there is a magical “this” keyword to refer to the current dictionary. A function defined as a slot in the dictionary uses this to get at other slots in the dictionary. Python allows multiple inheritance, Javascript does not.
const Person = {
name: "Ram",
greet() {
console.log("Hello I am Ram")
}
}
function createPerson {
const obj = {}
obj.name = name;
obj.greet = function() {
console.log("Hello I am ${this.name}")
}
return obj
}Every object in JavaScript has a built-in property, which is called its prototype. The prototype is itself an object, so the prototype will have its own prototype, making what’s called a prototype chain. The chain ends when we reach a prototype that has null for its own prototype. When you try to access a property of an object: if the property can’t be found in the object itself, the prototype is searched for the property. If the property still can’t be found, then the prototype’s prototype is searched, and so on until either the property is found, or the end of the chain is reached, in which case undefined is returned.
The object called Object.prototype is the most basic prototype, that all objects have by default. The prototype of it is null so it’s at the end of the prototype chain. The Object.create() method creates a new object and allows you to specify an object that will be used as the new object’s prototype.
In JS, all functions have a property named prototype. When you call a function as a constructor, this property is set as the prototype of the newly constructed object. So if we set the prototype of a constructor, we can ensure that all objects created with that constructor are given that prototype.
Object.assign(Person.prototype, personPrototype) //assigns personPrototypeClass:
Classes are in fact special functions and just as you can define function expressions and function declarations, a class can be defined in two ways: a class expression or a class declaration. Like function expressions, class expressions may be anonymous, or have a name that’s different from the variable that it’s assigned to.
class Rectangle
{
constructor(width, height)
{
this.width = width;
this.height = height;
}
}
A class element can be characterized by three aspects:
- Kind: getter, setter, method, or field
- Location: static or instance
- Visibility: public or private
In addition, there are two special class element syntaxes with their own references.
constructorstatic initialization blocks
The constructor method is a special method for creating and initializing an object created with a class. There can only be one special method with the name construction in a class.
Arrays:
In JavaScript, arrays are not primitives but are instead Array objects.
lengthslice(start, end)returns a shallow copy of a portion of an array into a new array selected from start to end (exclusive) where start and end represent the index of items in that array.- map(x⇒x) creates a new array populated with the results of calling a provided function on every element in the calling array.