extends - JavaScript | MDN

archived 29 Apr 2016 19:02:19 UTC
Your Search Results

    extends

    The extends keyword is used in a class declarations or class expressions to create a class with a child of another class.

    Syntax

    class ChildClass extends ParentClass { ... }

    Description

    The extends keyword can be used to subclass custom classes as well as built-in objects.
    The .prototype of the extension must be an Object or null.

    Examples

    Using extends

    The first example creates a class called Square from a class called Polygon. This example is extracted from this live demo (source).
    class Square extends Polygon {
      constructor(length) {
        // Here, it calls the parent class' constructor with lengths
        // provided for the Polygon's width and height
        super(length, length);
        // Note: In derived classes, super() must be called before you
        // can use 'this'. Leaving this out will cause a reference error.
        this.name = 'Square';
      }
    
      get area() {
        return this.height * this.width;
      }
    
      set area(value) {
        this.area = value;
      } 
    }

    Using extends with built-in objects

    This example extends the built-in Date object. This example is extracted from this live demo (source).
    class myDate extends Date {
      constructor() {
        super();
      }
    
      getFormattedDate() {
        var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
        return this.getDate() + "-" + months[this.getMonth()] + "-" + this.getFullYear();
      }
    }

    Extending null

    Extending from null works like with a normal class, except that the prototype object does not inherit from Object.prototype.
    class nullExtends extends null {
      constructor() {}
    }
    
    Object.getPrototypeOf(nullExtends); // Function.prototype
    Object.getPrototypeOf(nullExtends.prototype) // null

    Specifications

    Specification Status Comment
    ECMAScript 2015 (6th Edition, ECMA-262)
    The definition of 'extends' in that specification.
    STStandard Initial definition.
    ECMAScript 2017 Draft (ECMA-262)
    The definition of 'extends' in that specification.
    DDraft  

    Browser compatibility

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
    Basic support 42.0 45 (45) ? ? ?
    Array subclassing 43.0 No support ? ? ?
    Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
    Basic support No support 45.0 (45) ? ? ? 42.0
    Array subclassing No support No support ? ? ? 43.0

    See also

    Document Tags and Contributors

     Contributors to this page: fscholz, rwaldron, devinivy, jpmedley, claudepache, paul.irish
     Last updated by: fscholz,
    See also
    1. JavaScript
    2. Tutorials:
    3. JavaScript Guide
      1. Introduction
      2. Grammar and types
      3. Control flow and error handling
      4. Loops and iteration
      5. Functions
      6. Expressions and operators
      7. Numbers and dates
      8. Text formatting
      9. Regular expressions
      10. Indexed collections
      11. Keyed collections
      12. Working with objects
      13. Details of the object model
      14. Iterators and generators
      15. Meta programming
    4. Introductory
      1. JavaScript basics
      2. JavaScript technologies overview
      3. Introduction to Object Oriented JavaScript
    5. Intermediate
      1. A re-introduction to JavaScript
      2. JavaScript data structures
      3. Equality comparisons and sameness
      4. Closures
    6. Advanced
      1. Inheritance and the prototype chain
      2. Strict mode
      3. JavaScript typed arrays
      4. SIMD types
      5. Memory Management
      6. Concurrency model and Event Loop
    7. References:
    8. Built-in objects
      1. Standard built-in objects
      2. Array
      3. ArrayBuffer
      4. Atomics
      5. Boolean
      6. DataView
      7. Date
      8. Error
      9. EvalError
      10. Float32Array
      11. Float64Array
      12. Function
      13. Generator
      14. GeneratorFunction
      15. Infinity
      16. Int16Array
      17. Int32Array
      18. Int8Array
      19. InternalError
      20. Intl
      21. Intl.Collator
      22. Intl.DateTimeFormat
      23. Intl.NumberFormat
      24. Iterator
      25. JSON
      26. Map
      27. Math
      28. NaN
      29. Number
      30. Object
      31. ParallelArray
      32. Promise
      33. Proxy
      34. RangeError
      35. ReferenceError
      36. Reflect
      37. RegExp
      38. SIMD
      39. SIMD.Bool16x8
      40. SIMD.Bool32x4
      41. SIMD.Bool64x2
      42. SIMD.Bool8x16
      43. SIMD.Float32x4
      44. SIMD.Float64x2
      45. SIMD.Int16x8
      46. SIMD.Int32x4
      47. SIMD.Int8x16
      48. SIMD.Uint16x8
      49. SIMD.Uint32x4
      50. SIMD.Uint8x16
      51. Set
      52. SharedArrayBuffer
      53. StopIteration
      54. String
      55. Symbol
      56. SyntaxError
      57. TypeError
      58. TypedArray
      59. URIError
      60. Uint16Array
      61. Uint32Array
      62. Uint8Array
      63. Uint8ClampedArray
      64. WeakMap
      65. WeakSet
      66. decodeURI()
      67. decodeURIComponent()
      68. encodeURI()
      69. encodeURIComponent()
      70. escape()
      71. eval()
      72. isFinite()
      73. isNaN()
      74. null
      75. parseFloat()
      76. parseInt()
      77. undefined
      78. unescape()
      79. uneval()
    9. Expressions & operators
      1. Expressions and operators
      2. Arithmetic operators
      3. Array comprehensions
      4. Assignment operators
      5. Bitwise operators
      6. Comma operator
      7. Comparison operators
      8. Conditional (ternary) Operator
      9. Destructuring assignment
      10. Expression closures
      11. Generator comprehensions
      12. Grouping operator
      13. Legacy generator function expression
      14. Logical Operators
      15. Object initializer
      16. Operator precedence
      17. Property accessors
      18. Spread operator
      19. class expression
      20. delete operator
      21. function expression
      22. function* expression
      23. in operator
      24. instanceof
      25. new operator
      26. new.target
      27. super
      28. this
      29. typeof
      30. void operator
      31. yield
      32. yield*
    10. Statements & declarations
      1. Statements and declarations
      2. Legacy generator function
      3. block
      4. break
      5. class
      6. const
      7. continue
      8. debugger
      9. default
      10. do...while
      11. empty
      12. export
      13. for
      14. for each...in
      15. for...in
      16. for...of
      17. function
      18. function*
      19. if...else
      20. import
      21. label
      22. let
      23. return
      24. switch
      25. throw
      26. try...catch
      27. var
      28. while
      29. with
    11. Functions
      1. Functions
      2. Arguments object
      3. Arrow functions
      4. Default parameters
      5. Method definitions
      6. Rest parameters
      7. getter
      8. setter
    12. Classes
      1. Classes
      2. constructor
      3. extends
      4. static
    13. Misc
      1. Lexical grammar
      2. JavaScript data structures
      3. Enumerability and ownership of properties
      4. Iteration protocols
      5. Strict mode
      6. Transitioning to strict mode
      7. Template literals
      8. Deprecated features
    14. New in JavaScript
      1. New in JavaScript
      2. ECMAScript 5 support in Mozilla
      3. ECMAScript 6 support in Mozilla
      4. ECMAScript 7 support in Mozilla
      5. Firefox JavaScript changelog
      6. New in JavaScript 1.1
      7. New in JavaScript 1.2
      8. New in JavaScript 1.3
      9. New in JavaScript 1.4
      10. New in JavaScript 1.5
      11. New in JavaScript 1.6
      12. New in JavaScript 1.7
      13. New in JavaScript 1.8
      14. New in JavaScript 1.8.1
      15. New in JavaScript 1.8.5
    15. Documentation:
    16. Useful lists
      1. All pages index
      2. Methods index
      3. Properties index
      4. Pages tagged "JavaScript"
    17. Contribute
      1. JavaScript doc status
      2. The MDN project
    0%
    10%
    20%
    30%
    40%
    50%
    60%
    70%
    80%
    90%
    100%