A collection of various java script stories(practical samples) that explain & demonstrate core java script concepts.
- typeof 42 is
numberand typeofnumberis string - typeof null is
objectbecause of bug - typeof undeclared or unassigned variable is also
undefined - variable declared using var in unreachable block is hoisted and considered as declared but unassigned
- Can use typeof to define polyfills for feature that may or may not exist
- Array elements with string index do not affect length
- Array unused slots are considered as empty-slots
- Object wrappers used as constructor wrap object over primitive
- Boolean object wrapper initialized as false, does not return a false when accessed directly
- Object wrappers used without
newbehave as normal primitive - Array constructor function behaves differently based on number of arguments
- Symbols - Demo
- Prototype of object subtypes(Array and Function) is the subtype itself
- JSON stringify converts undefined, function, recursive prop access to null
- JSON stringify implicit coercion using toJSON
- JSON stringify explicit coercion using replacer array
- JSON stringify explicit coercion using toJSON gets precedence over replacer function
- parseInt is tolerant while coercion is intolerant
- Implicit coercion examples of + operator
- string coercion using + depends on valueOf and using String() depends on toString
- operators -,*,/ always coerce the operands to number
- Usage of !! - Implement onlyOneTrue function
- Operators || and && always return value of one of the two operands
- JS coercion gotchas
- Javascript ,(comma) operator executes all expressions and returns the last one
- Array created via constructor only sets length property, indices are not initialized
- Array higher order functions(map, filter, forEach) only iterate through indices that are initialized
- Constructor function v/s Normal function
- IIFE - Immediately Invoked Function Expression. Function with params executed on declaration
- Closure - Local function value accessible outside the function
- Call array methods on objects using
apply - Call built-in methods with variable args using
apply - Create prototype chain using
apply - Method overriding using
call. Call parent method by passing context of child - Tagged template literals are special function calls invoked without parenthesis "("
- Arrow fns assume this to be same as the value of this at original invocation place
- Arrow fns lexically inherit this from surrounding scope
- Demo - Difference in behaviour of arrow fn & regular fn
- Syntactic alternative of arrow fns using self
- Use static variables in javascript function to implement singleton pattern(without
staticorclasskeyword) - Static functions are not directly accessible to instances
- Create object using es6 classes
- Class constructor cannot be invoked without
new - Create object using Object.create
- writable=false prop value cannot be changed
- Properties of nested object can be modified even if writable is false
- enumerable=false will make property not iterable though can read/write directly
- configurable=false prop cant be deleted and only writable can be changed enumerable,configurable cant be changed
- All object props can have getter & setter methods to get and set value
- Constructor fn prototype is an object that will be proto of all objects instantiated from that function
- Prototype properties are not copied to instance but are delegated
- New object assigned to prototype will only be reflected on instances created after that
- New objects created after changing prototype will have same proto as changed prototype
- Prototype prop is added to all instances. Instance prop is specific to instance
- Shadowing1: Data accessor prop found higher on prototype chain with writable:
truewill be added to main object - Shadowing2: Data accessor prop found higher on prototype chain with writable:
falsewill be ignored(error in strict mode). NO Shadowing - Shadowing3: Prop is a setter higher on prototype chain. Always setter will be called. NO Shadowing
- Shadowing4: Can force shadow property using defineProperty
- Shadowing5: Implicit shadowing
- Prototype chain created with functions adds prototype props with enumerable=true
- Prototype chain created with class adds prototype props with enumerable=false
- Parent - Child link(via Delegation) is created on prototype object AND NOT on actual object
- Parent - Child link(via Class) is created on prototype object AND ALSO on actual object
- super can be used in consice functions of plain objects
- Prototype chain is more like proto.proto than prototype.prototype
- Global object default binding is disabled in strict mode
- Global variables bound to global object in non-strict mode
- Implicit binding example
- Implicit binding is lost when function is passed as parameter
- Hard binding does not lose
thiscontext - Complete hard binding demo - Argument pass through and return value
- Implement custom
bindmethod usingapply - Constructor with object as return value overwrites the new object
- Explicit binding takes precedence over implicit binding
newbinding takes precedence over implicit binding- Explicit binding takes precedence over
newbinding - Proper custom
bindimplementation - Custom bind implementation (hacky one)
- Implement partially applied(curried) functions using custom bind
- Custom "ES6bind" implementation
- Function reference when evaluated loses the context
- Cannot call an arrow function with
new - Cannot return primitives from a function when called with
new
- Iterate through array values manually using Symbol.iterator
- Iterating through last value still returns done:false
- next() returns done:true when trying to fetch again after last value is fetched
- Symbol.iterator returns new instance of iterator each time
- Calling next on exhausted iterator returns {value: undefined, done: true}
- Custom iterator to infinitely loop through even numbers
- Use
returnto notify producer that consumption is over - Demo -
yield&return returnvalue is accessible when explicitly iteratedreturnvalue NOT accessible when auto nexting- Consumer can free generator midway if consumption is over
- Demo - Generators yielding to Promise based async methods
- Generator can accept values passed from iterators
- Iterator resumes executing generator after completing assignment of previous yield
- Generator can generate infinite values
- Babel transpiled version of generator function
- yield* delegates to first generator or iterable object
- Generate fibo series using recursion via yield*