# JS Tip of the Day: Static Members Are Inherited

**URL:** <https://forum.kirupa.com/t/js-tip-of-the-day-static-members-are-inherited/643120>\
**Category:** web dev\
**Created:** [April 16, 2020, 1:51pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-static-members-are-inherited/643120 "2020-04-16T13:51:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [April 16, 2020, 1:51pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-static-members-are-inherited/643120/1 "2020-04-16T13:51:52Z")

</div>

**Static Members Are Inherited**  
Version: ES2015  
Level: Intermediate

When using `extends` with `class`, you set up a relationship between two classes where one class inherits from another. This way, all instances of the subclass will inherit from the superclass.

```javascript
class Parent {
    isParent () {
        return true;
    }
}

class Child extends Parent {
    isChild() {
        return true;
    }
}

let manChild = new Child();
console.log(manChild.isChild()); // true
console.log(manChild.isParent()); // true (inherited)

```

This inheritance also extends (see what I did there?) to the classes themselves, where the subclass will inherit static members of the parent class.

```javascript
class Parent {
    static makesParents () {
        return true;
    }
}

class Child extends Parent {
    static makesChildren () {
        return true;
    }
}

console.log(Child.makesChildren()); // true
console.log(Child.makesParents()); // true (inherited)

```

When you don’t explicitly use `extends`, no statics are inherited, even though the class will implicitly inherit from Object.

```javascript
class ImplicitObject {}
console.log(ImplicitObject.fromEntries([['prop', 'value']]));
// TypeError: ImplicitObject.fromEntries is not a function

```

But, if you want the static members of Object to be inherited, you can extend it explicitly.

```javascript
class ExplicitObject extends Object {}
console.log(ExplicitObject.fromEntries([['prop', 'value']]));
// {prop: 'value'}

```

While static Object methods won’t recognize your subtype (`ExplicitObject.fromEntries()` won’t create ExplicitObject instances), statics from other types like Array do.

```javascript
class SubArray extends Array {}
console.log(SubArray.from([]) instanceof SubArray); // true

```

When creating your own static methods, you may want to take this into consideration using `this` to refer to the current class rather than referring to it directly by name. Then subclasses inheriting that method will be using their own class rather than the original.

```javascript
class Boat {
    static create () {
        return new this(); // this rather than Boat
    }
}

class Sub extends Boat {}

console.log(Boat.create() instanceof Boat); // true
console.log(Sub.create() instanceof Sub); // true

```

More info:

- [extends on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/extends)
- [Object.fromEntries on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries)
- [Array.from on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from)
