Menu Close
    Convert your store into Multi Vendor Marketplace

    JavaScript classes, objects and methods

    Code-Snippet

    Hello,

    Here we learn how to create classes ,objects and methods in JavaScript.

    Classes are in fact “special functions”, and just as you can define function expressions and function declarations , the class syntax has two components: class expressions and function expressions .

    Class declarations:

    One way to define a class is using a class declaration. To declare a class, you use the class keyword with the name of the class.

    For Example

    class WkRectangle {
      constructor(length, width) {
        this.length = length;
        this.width = width;
      }
    }
    
    

    Class expressions:

    A class expression is another way to define a class. Class expressions can be named or unnamed. The name given to a named class expression is local to the class’s body.

    For Example:

    // unnamed
    var WkRectangle = class {
      constructor(length, width) {
        this.length = length;
        this.width = width;
      }
    };
    
    // named
    var WkRectangle = class WkRectangle {
      constructor(length, width) {
        this.length = length;
        this.width = width;
      }
    };
    
    

    Strict mode:

    The bodies of class declarations and class expressions are executed in strict mode.Strict mode  catches some common coding errors, throwing exceptions. It prevents, or throws errors, when relatively “unsafe” actions are taken (such as gaining access to the global object). It disables features that are confusing or poorly thought out.

    Constructor:

    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 “constructor” in a class. A Syntax error  will be thrown if the class contains more than one occurrence of a constructor method.

    A constructor can use the super keyword to call the constructor of a parent class.

    Prototype methods:

    For Example:

    class Polygon {
      constructor(height, width) {
        this.height = height;
        this.width = width;
      }
      
      get area() {
        return this.calcArea();
      }
    
      calcArea() {
        return this.height * this.width;
      }
    }
    
    const square = new Polygon(10, 10);
    
    console.log(square.area);
    
    

    Static methods:

    The static  keyword defines a static method for a class. Static methods are called without instantiating their class and are also not callable when the class is instantiated. Static methods are often used to create utility functions for an application.

    For Example:

    class Point {
        constructor(x, y) {
            this.x = x;
            this.y = y;
        }
    
        static distance(a, b) {
            const dx = a.x - b.x;
            const dy = a.y - b.y;
    
            return Math.sqrt(dx*dx + dy*dy);
        }
    }
    
    const p1 = new Point(5, 5);
    const p2 = new Point(10, 10);
    
    console.log(Point.distance(p1, p2));
    
    
    

    So we can create  and use classes ,objects and methods in JavaScript as above define ways.

    Thank you

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top