Skip to content

Instantly share code, notes, and snippets.

@yogain123
Created December 10, 2017 21:07
Show Gist options
  • Save yogain123/6374decfc71d648eeb3c4a4052a9789c to your computer and use it in GitHub Desktop.
Save yogain123/6374decfc71d648eeb3c4a4052a9789c to your computer and use it in GitHub Desktop.
Mordern JavaScript
@yogain123
Copy link
Author

Sets and ranges [...]

For instance, [eao] means any of the 3 characters: 'a', 'e', or 'o'.

Screen Shot 1941-10-09 at 7 40 56 PM

@yogain123
Copy link
Author

Quantifiers +, *, ? and {n}

Let’s say we have a string like +7(903)-123-45-67 and want to find all numbers in it. But unlike before, we are interested not in single digits, but full numbers: 7, 903, 123, 45, 67.

A number is a sequence of 1 or more digits \d. To mark how many we need, we can append a quantifier.

Screen Shot 1941-10-09 at 7 42 52 PM



Screen Shot 1941-10-09 at 7 43 08 PM



Screen Shot 1941-10-09 at 7 43 59 PM



Screen Shot 1941-10-09 at 7 44 07 PM

@yogain123
Copy link
Author

yogain123 commented Dec 30, 2019

Alternation (OR) |

Screen Shot 1941-10-09 at 7 46 06 PM

let res = /^(abc){3}$/g.test("abcabcabc")
console.log(res);   // true

@yogain123
Copy link
Author

yogain123 commented Dec 31, 2019

Methods of RegExp and String

  • str.match(regexp)
  • str.split(regexp|substr, limit)
  • str.search(regexp)
  • str.replace(str|regexp, str|func)
  • regexp.exec(str)
  • regexp.test(str)

@yogain123
Copy link
Author

BigInt

let bb = 234342134343213243424534232134253n;
let pp = BigInt("45443324254342425343242534232332");
let cc = BigInt(34234325443324254342425343242534232332);
console.log(typeof bb); // BigInt

Screenshot 2020-01-02 at 2 36 27 PM



Screenshot 2020-01-02 at 2 36 39 PM



Screenshot 2020-01-02 at 2 36 54 PM



Screenshot 2020-01-02 at 2 37 03 PM

@yogain123
Copy link
Author

JS 2020 features

console.log(obj.name ?? "yogendra"); // ?? same as || but only takes null, undefined as false value

console.log(obj?.name?.firstName)

Promise.allSettled([p1. p2])

console.log(globalThis) == console.log(self) == console.log(window) == console.log(global)
now globalThis is common for both browser and node

@yogain123
Copy link
Author

Shadow DOM

Shadow DOM serves for encapsulation. It allows a component to have its very own “shadow” DOM tree, that can’t be accidentally accessed from the main document, may have local style rules, and more.

Screenshot 2020-01-06 at 12 14 56 PM

@yogain123
Copy link
Author

yogain123 commented Jan 8, 2020

Getter and Setter in JS

What makes them special is that we don't access data as a function. We access it just like we would any old property

  • You can check if new data is valid before setting a property
  • You can perform an action on the data which you are getting or setting on a property.
  • You can control which properties can be stored and retrieved.
var shout = {
    _message: "HELLO!",
    get message() {
      return this._message;
    },
    set message(value) {
      this._message = value.toUpperCase();
    }
  };
  shout.message = "This is sparta!";
  console.log(shout.message);  // THIS IS SPARTA!

//================================================================

  class Person{ 
    constructor(){
        this._message= "HELLO!"
    }
    get message() {
      return this._message;
    }
    set message(value) {
      this._message = value.toUpperCase()+" LOL";
    }
  }
  let p = new Person();
  p.message = "this is sparta!";
  console.log(p.message);  // THIS IS SPARTA! LOL
  

@yogain123
Copy link
Author

Symbol

Screenshot 2020-02-13 at 1 12 30 PM



Screenshot 2020-02-13 at 1 12 47 PM



Screenshot 2020-02-13 at 1 13 00 PM

@yogain123
Copy link
Author

yogain123 commented Feb 13, 2020

prototypal chaining

let arr = [1,2,3,4];
let obj = {}
let fn = ()=>2;
console.log(arr.__proto__.__proto__)
console.log(obj.__proto__)
console.log(fn.__proto__.__proto__)
console.log(Function.prototype.__proto__)
console.log(Array.prototype.__proto__)
console.log(Object.prototype)

All is same

@yogain123
Copy link
Author

console

Screen Shot 1941-11-29 at 8 32 25 PM

@yogain123
Copy link
Author

Proxy in JavaScript

It basically wraps our object and then pass the handler to it to override the existing method

Screen Shot 1941-12-02 at 8 52 55 PM

@yogain123
Copy link
Author

Promise Catching at the place

let p1 = new Promise((resolve, reject) => {
    resolve("done1")
});

let p2 =  new Promise((resolve, reject) => {
    resolve("done2");
})

let p3 =  new Promise((resolve, reject) => {
    reject("reject3")
}).catch((error) => { // catching error directly here so that in Promise.all it is considered as successs
    return undefined;    
});

Promise.all([p1,p2,p3]).then(res=>{
    console.log({res});  // { res: [ 'done1', 'done2', undefined ] } 
    
}).catch((error) => {
    console.log({error});
})

@yogain123
Copy link
Author

yogain123 commented Feb 23, 2020

what is output

var foo = 'bim'
var getFoo = function () { return foo }
foo = 'boum'
console.log(getFoo());  // boum

The answer is ‘boum’ because foo is changed to ‘boum’ on line 3, and function getFoo will return a variable foo within its scope.

@yogain123
Copy link
Author

yogain123 commented Feb 25, 2020

Output

In the following code, I expected both a and b to be 3. However, a is undefined and b is 3. Why?

(function(){
var a = b = 3;
})();

console.log(typeof a);//"undefined"
console.log(b);//3



Screen Shot 1941-12-06 at 8 16 36 PM

Actually it is

var a = 3;
b = 3

b = 3 is an assignment without a declaration. Since there is no b in the current execution context (the IIFE), JavaScript engine looks for b in the outer lexical environment. In this case, the outer environment is the global execution context.
Since b is not found in the global execution context either, and assuming non-strict mode, JavaScript implicitly creates b as a property of the global object (e.g., window.b in browsers). This makes b a global variable.

@yogain123
Copy link
Author

Destructuring

Screen Shot 2020-05-14 at 5 09 03 PM

@yogain123
Copy link
Author

yogain123 commented Mar 6, 2022

Prototype


class Hola{
    constructor(){
        this.name="yogendra";
        console.log(this);
    }

    static div(){
        console.log("div");
    }

    sum(){
        console.log("sum");
    }
}

const h = new Hola();

console.log(Object.getPrototypeOf(h))   // {constructor: ƒ, sum: ƒ}

console.log(h.__proto__)   // {constructor: ƒ, sum: ƒ}

console.log(Hola.prototype); // {constructor: ƒ, sum: ƒ}


@yogain123
Copy link
Author

yogain123 commented Aug 6, 2022

Array.at()

Get the value at that position -> index passed
const arr = ["jack","yogi","abc"]
console.log(arr.at(0)) // "jack"
console.log(arr.at(-1)) // "abc"

@yogain123
Copy link
Author

IMG_4256

@yogain123
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment