# Difference between "new createVector()" and "createVector()"

**URL:** <https://discourse.processing.org/t/difference-between-new-createvector-and-createvector/25056>\
**Category:** Beginners\
**Created:** [October 30, 2020, 6:47pm UTC](https://discourse.processing.org/t/difference-between-new-createvector-and-createvector/25056 "2020-10-30T18:47:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason210](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@Jason210](https://discourse.processing.org/u/Jason210)\
**Post date:** [October 30, 2020, 6:47pm UTC](https://discourse.processing.org/t/difference-between-new-createvector-and-createvector/25056/1 "2020-10-30T18:47:48Z")

</div>

Hi

Can someone explain in simple terms what the difference is between **createVector()** and **new createVector()?**

Jason

---

<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:** [October 30, 2020, 7:43pm UTC](https://discourse.processing.org/t/difference-between-new-createvector-and-createvector/25056/2 "2020-10-30T19:43:29Z")

</div>

Never seen the keyword `new` used on the p5’s method **createVector()**:

- [p5js.org/reference/#/p5/createVector](http://p5js.org/reference/#/p5/createVector)

AFAIK keyword `new` has no effect whatsoever on method **createVector()**.

---

<div class="post-metadata">

**Author:** ![Jason210](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@Jason210](https://discourse.processing.org/u/Jason210)\
**Post date:** [October 30, 2020, 10:31pm UTC](https://discourse.processing.org/t/difference-between-new-createvector-and-createvector/25056/3 "2020-10-30T22:31:00Z")

</div>

That’s what I thought. No effect. There is none that I could determine. I wrote it be accident and it worked, without complaint, so I became curious.

---

<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:** [October 30, 2020, 10:51pm UTC](https://discourse.processing.org/t/difference-between-new-createvector-and-createvector/25056/4 "2020-10-30T22:51:42Z")

</div>

> [@Jason210](#):
>
> … and it worked, without complaint, so I became curious.

The keyword operator `new` creates an empty object and passes it to the invoked function becoming its internal keyword `this`.

By default that created `new` object is returned by the function unless the function explicitly returns another object.

The latter is what happens to method **createVector()**.

It returns its own p5.Vector object and therefore the object created by `new` is simply discarded.

For more info on what happens when we use the keyword operator `new` go to its reference link below:

> **[new - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new)**
>
> The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.

---

<div class="post-metadata">

**Author:** ![Jason210](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@Jason210](https://discourse.processing.org/u/Jason210)\
**Post date:** [October 31, 2020, 7:41am UTC](https://discourse.processing.org/t/difference-between-new-createvector-and-createvector/25056/5 "2020-10-31T07:41:57Z")

</div>

Thank you! That explains why the results are the same then.

---

<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:** [October 31, 2020, 9:07am UTC](https://discourse.processing.org/t/difference-between-new-createvector-and-createvector/25056/6 "2020-10-31T09:07:46Z")

</div>

An important addendum! If we happen to be using instance mode in place of global mode:

- [GitHub.com/processing/p5.js/wiki/Global-and-instance-mode](http://GitHub.com/processing/p5.js/wiki/Global-and-instance-mode)

The keyword `new` will actually change the behavior of method **createVector()** by forcing its internal keyword `this` to point to the plain object created by `new`.

Let’s say we have a parameter named _p_ representing an instance of class p5.

When we invoke `p.createVector();` its internal keyword `this` is gonna be equal to parameter _p_.

However if we use keyword `new` as in `new p.createVector;`, its internal `this` is gonna be equal to the object created by `new` instead of _p_!

And I’m suspicious that even in global mode `new createVector;` ends up replacing the global `this` by the `this` created by `new` as well.

BtW, an instance of p5.Vector object w/o a valid reference to p5 isn’t affected by **angleMode()**:

- [p5js.org/reference/#/p5.Vector](http://p5js.org/reference/#/p5.Vector)
- [p5js.org/reference/#/p5/angleMode](http://p5js.org/reference/#/p5/angleMode)

Default mode RADIANS is used instead.

For example, method p5.Vector::**heading()** invoked by a p5.Vector instance created via `new` like this `new p.createVector;` or even `new createVector;` will return its result in RADIANS regardless the sketch’s current **angleMode()**:

- [p5js.org/reference/#/p5.Vector/heading](http://p5js.org/reference/#/p5.Vector/heading)

---

<div class="post-metadata">

**Author:** ![Jason210](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@Jason210](https://discourse.processing.org/u/Jason210)\
**Post date:** [October 31, 2020, 5:45pm UTC](https://discourse.processing.org/t/difference-between-new-createvector-and-createvector/25056/7 "2020-10-31T17:45:17Z")

</div>

Well, thanks once again. What complicated mess. As a rule of thumb, I guess we generally don’t use **new** when creating new p5 variables?
