-
-
Save SimoneCheng/ab3e43ce8b2fbf2a4a933debafdf1387 to your computer and use it in GitHub Desktop.
Revisions
-
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -630,7 +630,7 @@ Notes: * happens in logical operations * Methods of primitives: * Primitives except `null` and `undefined` provide many helpful methods. * These methods work via temporary wrapper objects, but JavaScript engines are well tuned to optimize that internally. * Like `'hello'.toUpperCase()`, `13.123.toFixed(2)` * `new Number(10)` returns an `object` not primitive. -
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 84 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -385,12 +385,93 @@ outer scope so we can call them from any member function, but they will be execu * When creating the private methods, bound them to the current context using `.bind(this)`. * Call the private methods using `.call` or `.apply` with `this` as the context. * Define a reference variable `var self = this` to the current object inside the constructor function and use it inside the private methods instead of `this`. * For the `Factory class pattern` we can define a reference to the returned object inside the factory function and use it instead of `this` with above options. Functional pattern example: ```js function User(firstName, lastName) { var self = this; this.firstName = firstName; this.lastName = lastName; function getFirstName() { return self.firstName; } function getLastName() { return self.lastName; } this.getFullName = function() { return getFirstName() + ' ' + getLastName(); } } var user = new User('Ahmad', 'Fares'); console.log(user.getFullName()); ``` Factorial pattern example: ```js function createUser(firstName, lastName) { var self = {}; self.firstName = firstName; self.lastName = lastName; function getFirstName() { return self.firstName; } function getLastName() { return self.lastName; } self.getFullName = function() { return getFirstName() + ' ' + getLastName(); } return self; } var user = createUser('Ahmad', 'Fares') console.log(user.getFullName()); ``` Prototype-based pattern example: ```js function User(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } User.prototype._getFirstName = function() { return this.firstName; } User.prototype._getLastName = function() { return this.lastName; } User.prototype.getFullName = function() { return this._getFirstName() + ' ' + this._getLastName(); } var user = new User('Ahmad', 'Fares'); console.log(user.getFullName()); ``` ### Inheritance -
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 18 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -363,21 +363,36 @@ Object.create(null) * Create an object using `new` and function constructor that has everything inside it. * Public properties are defined inside the constructor function. * Public methods are defined inside the constructor function. * Can have `private` methods/properties that are not accessed from the outside. * Factory class pattern. * Create an object by calling a function that returns a new object. * Public properties are defined inside the returned object. * Public methods are defined inside the returned object. * Can have `private` methods that are not accessed from the outside. * Prototype-based class pattern. * Create an object using `new` and function constructor. * Public properties are defined inside the constructor function. * Public methods are defined inside the constructor's prototype property. * Can't have `private` methods, prefixing with `_` is a convention to indicate that a member should not be used from the outside. * The constructor only initializes the current object state. * More memory-efficient because all methods are shared between all objects. * Is the best and mostly used. Note: * For the `Functional class pattern` the `private` methods are simply defined in the scope of the constructor function, so any public member method will have an access to outer scope so we can call them from any member function, but they will be executed in different context (doesn't have an access to the members of the current object `this`), to solve that we have more than one option: * When creating the private methods, bound them to the current context using `.bind(this)`. * Call the private methods using `.call` or `.apply` with `this` as the context. * Define a reference variable to the current object inside the constructor function and use it inside the private methods. * For the `Factory class pattern` we can go with the second option. ```js var self = this; ``` ### Inheritance * Classical model -
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -323,7 +323,6 @@ var user = new function() { * To access the `[[Prototype]]`: * Use the non-standard property `__proto__`. * Use ES2015 `Object.getPrototypeOf()`, `Object.setPrototypeOf()`. * `__proto__` is setter/getter for the `[[Prototype]]`, defined in `Object.prototype`. * If an object that its prototype chain not end with the `Object`, the `__proto__` will not be avaliable. * The `object instanceof contructor` operator examines the prototype chain for the check. -
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -284,7 +284,7 @@ Array.prototype.slice.call(arguments); * Any function can be run with `new`. * To check if the function is called with `new` or without we can use `new.target` inside it. To allow creating an object with `new` or without (not recommended): ```js function User(name) { -
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -92,7 +92,7 @@ This document is written to help JavaScript developers to understand JavaScript' * An example of `Pyramid of Doom`. ### Promise * Represents a value that either avaliable or will be avaliable in later. * Represents the completion or failure of an a deferred or asynchronous operation, and its resulting value. * The function passed with the arguments `resolve` and `reject` called the `executor` function. * Can either be: -
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -89,7 +89,7 @@ This document is written to help JavaScript developers to understand JavaScript' ### Callback Hell * Deeply nested callbacks. * An example of `Pyramid of Doom`. ### Promise * Represents a value that either avaliable or will be avaliable in the future. -
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -35,13 +35,13 @@ This document is written to help JavaScript developers to understand JavaScript' * High level explanation * Can be devided to * Creation phase * Creates a variable object: * Variables declarations. * Function declarations. * Arguments. * Create the scope chain. * Determine the value of `this`. * Execution phase: * The code is interpreted and executed. * Lexical Environment: * Is a specification type. -
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 23 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,7 +15,7 @@ This document is written to help JavaScript developers to understand JavaScript' # Concepts And Terms ### Execution Context * Active execution contexts logically form the call stack. * The top execution context on the call stack is the running execution context. * The interpreter starts by the global execution context. @@ -49,12 +49,15 @@ This document is written to help JavaScript developers to understand JavaScript' * Consists of: * `Environment Record`: identifier bindings. * `outer` reference to the outer lexical environment or `null`. ### Context * Is the value of `this` which is a reference to the object that owns the current executing code. * Determined by how a function is invoked. ### Scope * The region where the binding between a name (like variable name) to an entity is valid. ### Closure * A closure is a function that remembers its outer variables and can access them. * Combination of a function and the lexical environment within which that function was declared * The `closure` is the function object itself. @@ -63,27 +66,32 @@ This document is written to help JavaScript developers to understand JavaScript' * JavaScript engines also may optimize, discard variables that are unused to save memory. * A `Lexical Environment` object lives in the `heap` as long as there is a function which may use it. And when there are none, it is cleared. * All functions in JavaScript are closures. * The internal property `[[Environment]]` of a function, refers to the outer lexical environment ### IIFE * Immediately-invoked function expressions. * A design pattern used by most popular libraries to place all library code inside of a local scope. * No global property is created for the function (anonymous function expression). * All of the properties created inside of the function expression are scoped locally. * Encapsulation, preserve the global namespace as any variables declared within the function body will be local to the closure but will still live throughout runtime. * Benefits: * Local scoping. ### Hoisting * Variable and function **declarations** are put into memory during the compile phase. * Stays exactly where you typed it in your coding (not actually moved to the top). * Only hoists declarations, not initializations. * Declarations contribute to the `VariableEnvironment` when the execution scope is entered (^). ### Callback * A callback function is a function passed into another function as an argument. * To be invoked inside the called function at some point, like after an **asynchronous** operation has completed. ### Callback Hell * Deeply nested callbacks. * Called also `Pyramid of Doom`. ### Promise * Represents a value that either avaliable or will be avaliable in the future. * Represents the completion or failure of an a deferred or asynchronous operation, and its resulting value. * The function passed with the arguments `resolve` and `reject` called the `executor` function. @@ -100,22 +108,24 @@ This document is written to help JavaScript developers to understand JavaScript' * `Promise.race(iterable)` * `Promise.resolve(value)` returns a resolved promise with the given value. * `Promise.reject(reason)` returns a rejected promise with the error. ### Pollyfills * Scripts that **fill in** the gap and add missing implementations for JavaScript or modifing the existing onces to support the modern standard. * Use the same API. * Like pollyfill for the `Function.prototype.bind` which is not supported in `IE8` or for promises. * Or pollyfills for new methods in ES6 to be used in all browsers. * Examples: * Babel polyfill. * polyfill.io. ### Fallback * Replaces the feature with: * Simplified functionality * Or Third-party plugin * Or an error message * Like if the browser doesn't support the video tag, replace it with flash plugin. ### Notes * Every function invocation has both a scope and a context associated with it. * When an execution context is created its `LexicalEnvironment` and `VariableEnvironment` components initially have the same value. -
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -50,11 +50,9 @@ This document is written to help JavaScript developers to understand JavaScript' * `Environment Record`: identifier bindings. * `outer` reference to the outer lexical environment or `null`. * Context: * Is the value of `this` which is a reference to the object that owns the current executing code. * Determined by how a function is invoked. * Scope: * The region where the binding between a name (like variable name) to an entity is valid. * Closure: * A closure is a function that remembers its outer variables and can access them. -
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,7 @@ This document is written to help JavaScript developers to understand JavaScript' * If `src` attribute is set, the content is ignored. * JavaScript interprets the line break as an **implicit** semicolon (not always). # Concepts And Terms * Execution Context: * Active execution contexts logically form the call stack. -
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,7 @@ This document is written to help JavaScript developers to understand JavaScript' * If `src` attribute is set, the content is ignored. * JavaScript interprets the line break as an **implicit** semicolon (not always). # Terms * Execution Context: * Active execution contexts logically form the call stack. -
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,8 @@ # Javascript Deep Dive This document is written to help JavaScript developers to understand JavaScript's weird parts deeply and to prepare for interviews, the following resources was really helpful to write this document: * [ECMAScript 6 Language specification](http://www.ecma-international.org/ecma-262/6.0/index.html) * [ECMAScript 5 Language specification](http://es5.github.io/#x10.3) * [MDN](https://developer.mozilla.org/bm/docs/Web/JavaScript) * [The Modern JavaScript Tutorial](https://javascript.info) @@ -104,7 +105,7 @@ This document is written to help JavaScript developers to understand JavaScript' * Pollyfills: * Scripts that **fill in** the gap and add missing implementations for JavaScript or modifing the existing onces to support the modern standard. * Use the same API. * Like pollyfill for the `Function.prototype.bind` which is not supported in `IE8` or for promises. * Or pollyfills for new methods in ES6 to be used in all browsers. * Examples: * Babel polyfill. -
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 24 additions and 23 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -167,7 +167,7 @@ task1() Bind pollyfill: ```js if (!('bind' in Function.prototype)) { Function.prototype.bind = function() { var func = this; @@ -212,7 +212,7 @@ Creates a new function by fixing some parameters of the existing one. A wrapper function that passes everything it gets to another one. ```js function wrapper() { return printArgs.apply(this, arguments); } @@ -222,7 +222,7 @@ function wrapper() { Using a method from another object on our object. ```js Array.prototype.slice.call(arguments); ``` @@ -277,7 +277,7 @@ Array.prototype.slice.call(arguments); To allow creating an object with `new` or without: ```js function User(name) { // Not called with new @@ -292,7 +292,7 @@ function User(name) { The `new function() { … }` pattern to encapsulate the creation of a single object: ```js var user = new function() { this.name = 'Anas'; this.age = 'Ali'; @@ -324,19 +324,19 @@ var user = new function() { Creates an empty object with given proto as `[[Prototype]]` (can be null) and optional property descriptors. ```js Object.create(proto[, descriptors]) ``` We can use `Object.create` to perform an object cloning: ```js var clone = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj)); ``` To create an empty object without a prototype ```js Object.create(null) ``` @@ -450,7 +450,7 @@ Notes: * symbol * For unique identifiers. ```js typeof undefined // "undefined" typeof 0 // "number" typeof true // "boolean" @@ -565,39 +565,39 @@ Notes: * AND returns the **first** falsy value or the **last** value if none were found. * The precedence of the `&&` is higher than `||`. ```js (x > 0) && alert( 'Greater than zero!' ); ``` ```js null || 2 || undefined // 2 ``` ```js alert(1) || 2 || alert(3) // 2, since alert evaluated to undefined ``` ```js 1 && null && 2 // null ``` ### Arrow function examples Multiple arguments ```js var sum = (a, b) => a + b; ``` Single argument ```js var double = n => n * 2; ``` Multiline arrow functions ```js var double = (n) => { return n * 2; }; @@ -614,7 +614,8 @@ Notes: * A variable declared inside a function is only visible inside that function. * The function is an object that its prototype points to `Function.prototype` ```js hello.__proto__ == Function.prototype ``` @@ -631,19 +632,19 @@ hello.__proto__ == Function.prototype Makes it impossible to accidentally create global variables. ```js mistypeVariable = 17; ``` Throws an error when trying to delete undeletable property. ```js delete Object.prototype; ``` Throws an error when trying to change unwritable property. ```js var person = {}; Object.defineProperty(person, 'age', { @@ -656,15 +657,15 @@ person.age = 13; // TypeError Throws an error when the a function arguments names are not unique. ```js function func(a, a) { console.log(a); } ``` No longer possible to reference the `global` object through `this` inside a function. ```js function func() { console.log(this); // undefined } @@ -674,7 +675,7 @@ func(); The `caller`, `callee`, and `arguments` properties may not be accessed on strict mode functions. ```js function func() { console.log(arguments.callee); // TypeError } -
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -126,7 +126,7 @@ Notes: Promises example: ```js function task1() { return new Promise(function(resolve, reject) { -
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 41 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -124,6 +124,47 @@ Notes: * The value of the `LexicalEnvironment` component may change during execution of code within an execution context like when entering a block. * Every run for a loop block has a separate `Lexical Environment`. Promises example: ``` function task1() { return new Promise(function(resolve, reject) { setTimeout(function() { console.log('Task1 is completed'); resolve(); }, 1100); }); } function task2() { return new Promise(function(resolve, reject) { setTimeout(function() { console.log('Task2 is completed'); resolve(); }, 1200); }); } function task3() { return new Promise(function(resolve, reject) { setTimeout(function() { console.log('Task3 is completed'); resolve(); }, 1300); }); } task1() .then(task2) .then(task3); ``` Bind pollyfill: ``` -
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -86,8 +86,7 @@ This document is written to help JavaScript developers to understand JavaScript' * Called also `Pyramid of Doom`. * Promise: * Represents a value that either avaliable or will be avaliable in the future. * Represents the completion or failure of an a deferred or asynchronous operation, and its resulting value. * The function passed with the arguments `resolve` and `reject` called the `executor` function. * Can either be: * Fulfilled with a **value**. -
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -78,7 +78,7 @@ This document is written to help JavaScript developers to understand JavaScript' * Stays exactly where you typed it in your coding (not actually moved to the top). * Only hoists declarations, not initializations. * Declarations contribute to the `VariableEnvironment` when the execution scope is entered (^). * Callback: * A callback function is a function passed into another function as an argument. * To be invoked inside the called function at some point, like after an **asynchronous** operation has completed. * Callback Hell: -
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ This document is written to help JavaScript developers to understand JavaScript' # Hard Terms * Execution Context: * Active execution contexts logically form the call stack. * The top execution context on the call stack is the running execution context. * The interpreter starts by the global execution context. @@ -65,7 +65,7 @@ This document is written to help JavaScript developers to understand JavaScript' * A `Lexical Environment` object lives in the `heap` as long as there is a function which may use it. And when there are none, it is cleared. * All functions in JavaScript are closures. * The internal property `[[Environment]]` of a function, refers to the outer lexical environment. * IIFE: * Immediately-invoked function expressions. * A design pattern used by most popular libraries to place all library code inside of a local scope. * No global property is created for the function (anonymous function expression). @@ -84,7 +84,7 @@ This document is written to help JavaScript developers to understand JavaScript' * Callback Hell: * Deeply nested callbacks. * Called also `Pyramid of Doom`. * Promise: * Represents a value that either avaliable or will be avaliable in the future. * Represents the completion or failure of an asynchronous operation, and its resulting value. * Represents placeholder for the eventual results of a deferred (and possibly asynchronous) computation. -
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 4 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -93,15 +93,10 @@ This document is written to help JavaScript developers to understand JavaScript' * Fulfilled with a **value**. * Rejected with a **reason** (error). * Internal properties of promise instances: * `[[PromiseResult]]` the value or the error. * `[[PromiseState]]` can be: `pending`, `fulfilled` or `rejected`. * `[[PromiseFulfillReactions]]` queue for `then` handlers. * `[[PromiseRejectReactions]]` queue for `catch` handlers. * Methods: * `Promise.all(iterable)` * `Promise.race(iterable)` -
faressoft revised this gist
Oct 6, 2017 . 1 changed file with 29 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -78,6 +78,35 @@ This document is written to help JavaScript developers to understand JavaScript' * Stays exactly where you typed it in your coding (not actually moved to the top). * Only hoists declarations, not initializations. * Declarations contribute to the `VariableEnvironment` when the execution scope is entered (^). * Callbacks: * A callback function is a function passed into another function as an argument. * To be invoked inside the called function at some point, like after an **asynchronous** operation has completed. * Callback Hell: * Deeply nested callbacks. * Called also `Pyramid of Doom`. * Promise * Represents a value that either avaliable or will be avaliable in the future. * Represents the completion or failure of an asynchronous operation, and its resulting value. * Represents placeholder for the eventual results of a deferred (and possibly asynchronous) computation. * The function passed with the arguments `resolve` and `reject` called the `executor` function. * Can either be: * Fulfilled with a **value**. * Rejected with a **reason** (error). * Internal properties of promise instances: * `[[PromiseResult]]` * `[[PromiseState]]` can be: * pending * fulfilled * rejected * `[[PromiseFulfillReactions]]` * Queue for `then` handlers. * `[[PromiseRejectReactions]]` * Queue for `catch` handlers. * Methods: * `Promise.all(iterable)` * `Promise.race(iterable)` * `Promise.resolve(value)` returns a resolved promise with the given value. * `Promise.reject(reason)` returns a rejected promise with the error. * Pollyfills: * Scripts that **fill in** the gap and add missing implementations for JavaScript or modifing the existing onces to support the modern standard. * Use the same API. -
faressoft revised this gist
Oct 5, 2017 . 1 changed file with 7 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,11 @@ # Javascript Deep Dive This document is written to help JavaScript developers to understand JavaScript's weird parts deeply and to prepare for interviews, the following resources was really helpful to write this document: * [ECMAScript Language specification](http://es5.github.io/#x10.3) * [MDN](https://developer.mozilla.org/bm/docs/Web/JavaScript) * [The Modern JavaScript Tutorial](https://javascript.info) # General Notes * No attribute is required for the `script` element. * The benefit of a separate file is browser caching. @@ -116,7 +116,7 @@ if (!('bind' in Function.prototype)) { } ``` # Functions Tricks ### Decorator @@ -344,7 +344,7 @@ Notes: * prototype.constructor * \_\_proto\_\_: Function.prototype # Scheduling with Timers * Using `setTimeout` and `setInterval` functions. * `setTimeout` can be used to implement `setInterval`. @@ -466,7 +466,7 @@ Notes: * Like `'hello'.toUpperCase()`, `13.123.toFixed(2)` * `new Number(10)` returns an `object` not primitive. # Object to Primitive Conversion * All objects will become `true` if converted to boolean. * To convert to string, the `toString()` methods will be used or `valueOf().` @@ -493,7 +493,7 @@ Notes: * `undefined < 0` is false. * `undefined <= 0` is false. # Logical Operators * Or (`||`) * OR returns the **first** truthy value or the **last** one if no such value is found. @@ -554,7 +554,7 @@ Notes: hello.__proto__ == Function.prototype ``` # Use Strict * Applies to entire scripts or to individual functions. * Strict mode changes semantics. Relying on those changes will cause mistakes and errors in browsers which don't implement strict mode. -
faressoft revised this gist
Oct 5, 2017 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -170,8 +170,8 @@ Array.prototype.slice.call(arguments); * The dot notation: `obj.key` * The square brackets notation `obj[key]` * An empty object can be created using: * Using the object constructor `new Object()`. * Using the object literal `{}`. * To remove a property we can use the `delete` operator. * Existence check * Using `typeof`, `typeof obj.key !== 'undefined'`. -
faressoft revised this gist
Oct 5, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -165,7 +165,7 @@ Array.prototype.slice.call(arguments); # Object * Objects are associative arrays, stores key-value pairs. * Keys must be `string` or `symbol`, values can be anything. * To access a property, we can use: * The dot notation: `obj.key` * The square brackets notation `obj[key]` -
faressoft revised this gist
Oct 5, 2017 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -182,6 +182,7 @@ Array.prototype.slice.call(arguments); * `Object.assign(dest[, src1, src2, src3...]);` * `Object.assign({}, user);` * `Object.assign` not supported in `IE`, we can use `for..in`. * `var clone = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));` * Deep copy * `_.cloneDeep()` from [Lodash](https://lodash.com/) * There is other types of object other than the the `Object` (aka. `plain object`): @@ -266,7 +267,7 @@ Object.create(proto[, descriptors]) We can use `Object.create` to perform an object cloning: ``` var clone = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj)); ``` To create an empty object without a prototype -
faressoft revised this gist
Oct 5, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -183,7 +183,7 @@ Array.prototype.slice.call(arguments); * `Object.assign({}, user);` * `Object.assign` not supported in `IE`, we can use `for..in`. * Deep copy * `_.cloneDeep()` from [Lodash](https://lodash.com/) * There is other types of object other than the the `Object` (aka. `plain object`): * Array * Date -
faressoft revised this gist
Oct 5, 2017 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,12 +10,15 @@ This document is written to help JavaScript developers to understand JavaScript' * No attribute is required for the `script` element. * The benefit of a separate file is browser caching. * If `src` attribute is set, the content is ignored. * JavaScript interprets the line break as an **implicit** semicolon (not always). # Hard Terms * Execution Context * Active execution contexts logically form the call stack. * The top execution context on the call stack is the running execution context. * The interpreter starts by the global execution context. * Each invocation of a function, creates a new execution context appended to the top of the execution stack. * Low level explanation * Breaks into: * LexicalEnvironment @@ -93,9 +96,6 @@ This document is written to help JavaScript developers to understand JavaScript' Notes: * Every function invocation has both a scope and a context associated with it. * When an execution context is created its `LexicalEnvironment` and `VariableEnvironment` components initially have the same value. * The value of the `VariableEnvironment` component never changes. * The value of the `LexicalEnvironment` component may change during execution of code within an execution context like when entering a block. -
faressoft revised this gist
Oct 5, 2017 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,6 @@ This document is written to help JavaScript developers to understand JavaScript' * No attribute is required for the `script` element. * The benefit of a separate file is browser caching. * If `src` attribute is set, the content is ignored. # Hard Terms -
faressoft revised this gist
Oct 5, 2017 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,6 +15,8 @@ This document is written to help JavaScript developers to understand JavaScript' # Hard Terms * Execution Context * Active execution contexts logically form the call stack. * The top execution context on the call stack is the running execution context. * Low level explanation * Breaks into: * LexicalEnvironment -
faressoft revised this gist
Oct 5, 2017 . 1 changed file with 199 additions and 200 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,6 @@ # JavaScript Deep Dive This document is written to help JavaScript developers to understand JavaScript's weird parts deeply and to prepare for interviews, the following resources was really helpful to write this document: * [ECMAScript Language specification](http://es5.github.io/#x10.3) * [MDN](https://developer.mozilla.org/bm/docs/Web/JavaScript) * [The Modern JavaScript Tutorial](https://javascript.info) @@ -116,167 +115,50 @@ if (!('bind' in Function.prototype)) { } ``` # Working with functions ### Decorator Is a wrapper around a function that alters its behavior. Like caching the results or formatting them. To implement them we need: * `func.call(context, arg1, arg2…)` calls `func` with given context and arguments. * `func.apply(context, args)` calls `func` with given context and arguments passed as an array-like. * `func.bind(context, ...args)` returns a `bound variant` of function `func` that fixes the context and first arguments if given. Note: * `bind` is really useful with: * Setting the context for `setTimeout`'s callback function. * Partial functions. * Currying functions. ### Partial functions Creates a new function by fixing some parameters of the existing one. ### Currying functions * Translating a function from callable as `f(a, b, c)` into callable as `f(a)(b)(c)`. * To allow it to be **called normally** or **get partials**. ### Call forwarding A wrapper function that passes everything it gets to another one. ``` function wrapper() { return printArgs.apply(this, arguments); } ``` ### Method borrowing Using a method from another object on our object. ``` Array.prototype.slice.call(arguments); ``` # Object @@ -460,6 +342,179 @@ Notes: * prototype.constructor * \_\_proto\_\_: Function.prototype # Scheduling with timers * Using `setTimeout` and `setInterval` functions. * `setTimeout` can be used to implement `setInterval`. * They are not a part of JavaScript specification (ES). * Zero-timeout scheduling `setTimeout(...,0)`: * Is used to schedule the call as soon as possible, but after the current code is complete. * To split CPU-hungry tasks into pieces, so that the script doesn’t **hang**. * Let the browser do something else while the process is going on. # Data Types * Dynamically typed. * There are data types, but variables are not bound to any of them. * No `function` type, they belong to the object type, but `typeof` treats them differently. * An old bug in JavaScript is `typeof null == 'object'`. * Seven basic data type: * number * Serves both for integer and floating point numbers. * There are **special numeric values**: `Infinity`, `-Infinity` and `NaN`. * Maths is safe in JavaScript, will never stop with a fatal error. * `NaN` represents a computational error. * `1/0 == Infinity`; * `-1/0 == -Infinity`; * `"hello" / 2` evaluated to `NaN`. * string * Encoded using UTF-16. * Immutable, impossible to change a character. * boolean * null * A standalone type that has a single value `null`. * To indicate `empty` or `unknown values`. * undefined * A standalone type that has a single value `undefined`. * To indicate `value not assigned` * Only used for checks. * object * Not a primitive type. * Collection of data. * symbol * For unique identifiers. ``` typeof undefined // "undefined" typeof 0 // "number" typeof true // "boolean" typeof "foo" // "string" typeof Symbol("id") // "symbol" typeof Math // "object" typeof null // "object" typeof alert // "function" ``` # Type Conversion ### Implicit conversion * `alert` automatically converts any value to a string. * Mathematical operations convert values to numbers. * If condition expression converted to boolean. ### Explicit conversion #### To String * `String(null)` become `'null'` * `String(undefined)` become `'undefined'` * `String(false)` become `'false'` * `String(true)` become `'false'` * `String(10.1)` become `'10.1'` #### To Number * `Number(null)` become `0` * `Number(undefined)` become `NaN` * `Number(false)` become `0` * `Number(true)` become `1` * `Number('10.1')` become `10.1` * `Number(' 10.1 \t\n ')` become `10.1` * `Number(' 10.1z \t\n ')` become `NaN` * `Number(' \t\n ')` become `0` * `Number('')` become `0` Notes: * To convert a string into a number * Trim whitespaces (spaces, tabs, new lines). * An empty string becomes `0` * An error gives `NaN`. * Concatenation operation vs plus * `1 + 2 + 'a' == '3a'` * `'a' + 1 + 2 == 'a12'` * Can convert to numbers with `+` and `-` also. * `+'10' == 10` * `-'10' == -10` #### To Boolean **False**: values that are intuitively **empty** * `Boolean(null)` become `false` * `Boolean(undefined)` become `false` * `Boolean('')` become `false` * `Boolean(0)` become `false` * `Boolean(NaN)` become `false` **True**: otherwise * `Boolean(' ')` become `true` * `Boolean('0')` become `true` * `Boolean('abc')` become `true` * `Boolean(123)` become `true` Notes: * happens in logical operations * Methods of primitives: * Primitives except `null` and `undefined` provide many helpful methods. * These methods work via temporary objects, but JavaScript engines are well tuned to optimize that internally. * Like `'hello'.toUpperCase()`, `13.123.toFixed(2)` * `new Number(10)` returns an `object` not primitive. # Object to primitive conversion * All objects will become `true` if converted to boolean. * To convert to string, the `toString()` methods will be used or `valueOf().` * To convert to number, the `valueOf()` methods will be used or `toString().` # Operators * The `,` comma operator. * `10,20 == 10` * Evaluates both, returns the last one. # Comparisons * Comparison operators return a logical value. * Strings are compared letter-by-letter in the “dictionary” order. * When values of different types are compared, they get converted to numbers (with the exclusion of a strict equality check). * Values `null` and `undefined` equal `==` each other and do not equal any other value. * `null == null` is true. * `null == undefined` is true. * `undefined == undefined` is true. * An incomparable undefined, same as `NaN` (always false): * `undefined > 0` is false. * `undefined == 0` is false. * `undefined < 0` is false. * `undefined <= 0` is false. # Logical operators * Or (`||`) * OR returns the **first** truthy value or the **last** one if no such value is found. * And (`&&`) * AND returns the **first** falsy value or the **last** value if none were found. * The precedence of the `&&` is higher than `||`. ``` (x > 0) && alert( 'Greater than zero!' ); ``` ``` null || 2 || undefined // 2 ``` ``` alert(1) || 2 || alert(3) // 2, since alert evaluated to undefined ``` ``` 1 && null && 2 // null ``` ### Arrow function examples Multiple arguments @@ -497,62 +552,6 @@ Notes: hello.__proto__ == Function.prototype ``` # Use strict * Applies to entire scripts or to individual functions.
NewerOlder