# Accessing class methods

**URL:** <https://discourse.processing.org/t/accessing-class-methods/15415>\
**Category:** Coding Questions\
**Created:** [November 12, 2019, 2:19am UTC](https://discourse.processing.org/t/accessing-class-methods/15415 "2019-11-12T02:19:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zyxon](https://avatars.discourse-cdn.com/v4/letter/z/7ea924/32.png) [@Zyxon](https://discourse.processing.org/u/Zyxon)\
**Post date:** [November 12, 2019, 2:19am UTC](https://discourse.processing.org/t/accessing-class-methods/15415/1 "2019-11-12T02:19:14Z")

</div>

Hi,

I’m new to javascript so I’m not familiar with classes in javascript (I’m from Java). I declared a class like so:

```auto
class Obj {
	constructor(name, distance, description, colour, size) {
		//constructor
	}

	draw() {
		//stuff
	}
}

```

And then in the sketch’s draw method I attempt to call an `Obj` instance’s `draw` method:

```auto
function draw() {
     object.draw();
}

```

But I get the error:

> Uncaught TypeError: object.draw is not a function

What am I doing wrong here? Thanks for reading.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 12, 2019, 3:14am UTC](https://discourse.processing.org/t/accessing-class-methods/15415/2 "2019-11-12T03:14:59Z")

</div>

I can’t pinpoint anything wrong from your incomplete posted code. 😑  
But you can check out these 2 sketches below that use `class`: 😇

[OpenProcessing.org/sketch/717760#code](http://OpenProcessing.org/sketch/717760#code)

> **[Damper Balls](http://bl.ocks.org/GoSubRoutine/fa085945d45152786698f44a9523ccac)**
>
> GoSubRoutine’s Block fa085945d45152786698f44a9523ccac

  

> <https://gist.github.com/GoSubRoutine/fa085945d45152786698f44a9523ccac>

---

<div class="post-metadata">

**Author:** ![Zyxon](https://avatars.discourse-cdn.com/v4/letter/z/7ea924/32.png) [@Zyxon](https://discourse.processing.org/u/Zyxon)\
**Post date:** [November 12, 2019, 3:53am UTC](https://discourse.processing.org/t/accessing-class-methods/15415/3 "2019-11-12T03:53:00Z")

</div>

My code is a bit long so I didn’t want to paste it, here is the [link to my sketch](https://www.openprocessing.org/sketch/790582#code)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 12, 2019, 4:15am UTC](https://discourse.processing.org/t/accessing-class-methods/15415/4 "2019-11-12T04:15:00Z")

</div>

* * *

`objects.push(new Object("Sun", 0, "description", color(255, 204, 0), 500));`

Your `class` is called Obj, not Object, which is btW a JS builtin constructor:

> **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
>
> The Object constructor creates an object wrapper.

`objects.push(new Obj("Sun", 0, "description", color(255, 204, 0), 500));`

* * *

You’re using `for...in` here: `for (let object in objects) {`

> **[for...in](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in)**
>
> The for...in statement iterates over all non-Symbol, enumerable properties of an object.

Use `for...of` instead: `for (const object of objects) {`

> **[for...of](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of)**
>
> The for...of statement creates a loop iterating over iterable objects, including: built-in String, Array, array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined iterables. It invokes a custom iteration hook with...

* * *

P.S.: It’s advisable to load assets within **preload()**:  
[p5js.org/reference/#/p5/preload](http://p5js.org/reference/#/p5/preload)

* * *

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 12, 2019, 4:20am UTC](https://discourse.processing.org/t/accessing-class-methods/15415/5 "2019-11-12T04:20:05Z")

</div>

-a-

```auto
	objects.push(new Obj("Sun", 0, "description", color(255, 204, 0), 500));

```

not `new Object()`

-b-

```auto
	for ( let i =0; i < objects.length; i++) objects[i].draw();

```

works well

---

<div class="post-metadata">

**Author:** ![Zyxon](https://avatars.discourse-cdn.com/v4/letter/z/7ea924/32.png) [@Zyxon](https://discourse.processing.org/u/Zyxon)\
**Post date:** [November 12, 2019, 4:38am UTC](https://discourse.processing.org/t/accessing-class-methods/15415/6 "2019-11-12T04:38:27Z")

</div>

@GoToLoop thank you for the comprehensive reply!

@kll Can’t believe I didn’t realize that I forgot to change the class name there … 🤦‍♂️
