# The Classy Way to Create Objects

**URL:** <https://forum.kirupa.com/t/the-classy-way-to-create-objects/636787>\
**Category:** programming\
**Created:** [June 20, 2017, 2:02am UTC](https://forum.kirupa.com/t/the-classy-way-to-create-objects/636787 "2017-06-20T02:02:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [June 20, 2017, 2:02am UTC](https://forum.kirupa.com/t/the-classy-way-to-create-objects/636787/1 "2017-06-20T02:02:05Z")

</div>

This is the discussion topic where you can post all your questions (and complaints) about using the new-ish class syntax to create objects: https://www.kirupa.com/javascript/classy\_way\_to\_create\_objects.htm :slight\_smile:

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [June 20, 2017, 5:10am UTC](https://forum.kirupa.com/t/the-classy-way-to-create-objects/636787/2 "2017-06-20T05:10:08Z")

</div>

I remember learning this stuff from senocular’s tutorial. It was a game changer once I understood what was going on. Ahh memories…

---

<div class="post-metadata">

**Author:** ![digitalmatt](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/digitalmatt/32/694_2.png) [@digitalmatt](https://forum.kirupa.com/u/digitalmatt)\
**Post date:** [June 22, 2017, 3:43am UTC](https://forum.kirupa.com/t/the-classy-way-to-create-objects/636787/3 "2017-06-22T03:43:03Z")

</div>

This is great thanks, Kirupa. I started here and worked my way back to your Intro to Objects article. I’ve been programming in JavaScript for 10+ years and I feel like I still have a lot to learn. Even refreshing with these fundamentals is so helpful.

---

<div class="post-metadata">

**Author:** ![Woodenshipandironman](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/woodenshipandironman/32/12138_2.png) [@Woodenshipandironman](https://forum.kirupa.com/u/Woodenshipandironman)\
**Post date:** [July 23, 2020, 4:02pm UTC](https://forum.kirupa.com/t/the-classy-way-to-create-objects/636787/4 "2020-07-23T16:02:51Z")

</div>

What do classes offer that functions cannot? I wrote the same piece of code from the article using functions. There is a `planetMaker` function and a `potatoPlanet` function which inherits from `planetMaker`. It seems cleaner and simpler this way, as it doesn’t require a lot of syntax (`this`, `extends`, `super`, `constructor`) and the code is reasonably well structured and readable. Why is this pattern not used more often?

```
var planetMaker = function (name, radius) {
	var gravity;
	return {
		getSurfaceArea: function () {
			return 4 * Math.PI * Math.pow(radius, 2);
		},
		setGravity: function (value) {
			gravity = value;
		},
		getGravity: function () {
			return gravity;
		}
	};
};

var potatoPlanet = function (name, width, potatoType) {
	var planet = planetMaker(name, width);
	
	planet.getPotatoType = function () {
		return potatoType;
	};
	return planet;
};

```

Everything works as expected:

```
var potatoe = potatoPlanet('spudnik', 12411, 'Russet');
potatoe.setGravity(9);
potatoe.getGravity();
9
potatoe.getPotatoType();
"Russet"
```

---

<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:** [July 23, 2020, 4:23pm UTC](https://forum.kirupa.com/t/the-classy-way-to-create-objects/636787/5 "2020-07-23T16:23:48Z")

</div>

1. Clear intent. When you see the `class` keyword, you know its intent is to make instances (with a few exceptions with abstract or static classes). As a structure, functions alone are capable of doing much more so in seeing a function in code, it’s not always clear that it’s designed to create new object instances.
2. Implicit returns. In the constructor, you don’t need to return an object. It is automatically returned for you keeping the constructor (a little) simpler.
3. Method sharing. When you create new instances of a class, new copies of methods aren’t created for each and every instance. Instead, only a single method exists that is shared among all instances of that class.
4. Method overrides. You can easily override methods of the same name from a superclass while still being able to easily access the original implementation through `super`.
5. Static inheritance. When extending another class, not only does inheritance come automatically for instances of the class, but static methods also get inherited. For example, `ArraySubclass.from()` will use the `from()` inherited from the `Array` superclass and create `ArraySubclass` instances when called.
6. Identity. Instances of classes have an association with their constructor. You can tell if an object is an instance of the constructor using the `instanceof` operator.
7. Fitting in. Classes are the canonical way for defining new custom object types in JavaScript. By using classes you can easily integrate with other systems such as Web Components (classes needed to extend HTMLElement, etc.) and have others easily integrate with your own code.

---

<div class="post-metadata">

**Author:** ![Woodenshipandironman](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/woodenshipandironman/32/12138_2.png) [@Woodenshipandironman](https://forum.kirupa.com/u/Woodenshipandironman)\
**Post date:** [July 23, 2020, 4:35pm UTC](https://forum.kirupa.com/t/the-classy-way-to-create-objects/636787/6 "2020-07-23T16:35:04Z")

</div>

Thank you, that was fast and informative. I guess that when you’re working with Canvas, point number 3 really matters in terms of speed.

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [April 20, 2021, 2:59am UTC](https://forum.kirupa.com/t/the-classy-way-to-create-objects/636787/7 "2021-04-20T02:59:45Z")

</div>

This one is one of the more popular articles on the site that doesn’t have a video version. Well, just fixed that 🩹

[![](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/7/7/7795c5e7eb1a92a55782fb2be8f6c2cac7b6eb9d.jpeg "Using Classes in JavaScript") ](https://www.youtube.com/watch?v=u21N28xS9jw)

😛
