javascript

    Metaprogramming

    02 Dec, 2023public

    Class in JS

    05 Dec, 2023public

    Promises

    13 Nov, 2023public

    Js notes

    11 Jan, 2024public

    Iteration in javascript

    15 Nov, 2023public
    economics

    Incentive

    21 Aug, 2023public
    demo

    Demo

    13 Nov, 2023public
    personal

    Teaching myself CS

    05 Dec, 2023public

    People

    12 Dec, 2023public

    Links

    25 Mar, 2024public
    computer graphics

    Computer graphics from scratch

    12 Dec, 2023public
    notag

    Flexbox practice

    14 Aug, 2024public
    Lying in wait, set to pounce on the blank page,are letters up to no good,clutches of clauses so subordinatethey'll never let her get away.
    - The Joy Of Writing, Wislawa Szymborska

    Class in JS

    class keyword in js is just a syntactic sugar
    .05 Dec, 2023

    class keyword in javascript is just a syntactic sugar. Let' see how cumbersome is it to create objects with some methods defined on them without it.

    function animal(name, numLegs) {
      // if you call animal without new keyword, this is the global object. 
      // if y
      this.name = name
      this.numLegs = numLegs
    }
    
    animal.prototype.greet = function () {
      return `I am a ${this.name}, I have ${this.numLegs} legs`
    }
    

    Creating instances of animal:

    const lion = new animal("lion", 4)
    const octopus = new animal("octopus", 8)
    
    console.log(octopus.greet())
    console.log(lion.greet())
    console.log(lion.hasOwnProperty("name"))
    

    Now, let's do the same thing using class

    class Animal {
      constructor(name, numLegs) {
        this.name = name
        this.numLegs = numLegs
      }
      greet() {
        
        return `I am a ${this.name}, I have ${this.numLegs} legs`
      }
    }
    
    
    const lion = new Animal("lion",4)
    const octopus = new Animal("octopus",8)
    

    console.log(lion.greet())
    console.log(octopus.greet())
    console.log(lion.hasOwnProperty("greet"))
    
    Table of contents
    Abhimanyu