Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla
Your Search Results

    class

    This article is in need of a technical review.

    This is an experimental technology, part of the ECMAScript 6 (Harmony) proposal.
    Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.

    Draft
    This page is not complete.

    Summary

    Starting with ECMASCript 6, a new syntax for class definitions is introduced. Although the syntax looks like the syntax of languages with class-based inheritance, JavaScript remains prototype-based.

    Syntax

    The class keyword has two forms, a declaration form and an expression form.

    //declaration form
    class className {
      //Class body
    };

     

    // expression form
    const MyClass = class className {
      //Class body
    };

    Example

    "use strict";
    
    class Polygon {
      constructor(height, width) {
        this.height = height;
        this.width = width;
      }
    }
    
    class Square extends Polygon {
      constructor(sideLength) {
        super(sideLength, sideLength);
      }
      get area() {
        return this.height * this.width;
      }
      set sideLength(newLength) {
        this.height = newLength;
        this.width = newLength;
      }
    }
    
    var square = new Square(2);

    Specifications

    Specification Status Comment
    ECMAScript 6 (ECMA-262)
    The definition of 'Class definitions' in that specification.
    Release Candidate Initial definition.

    Browser compatibility

     

    See also

    Document Tags and Contributors

    Contributors to this page: kscarfone, jpmedley
    Last updated by: jpmedley,