# A Javascript prototype question

**URL:** https://discourse.processing.org/t/a-javascript-prototype-question/24699
**Category:** Beginners
**Created:** [October 19, 2020, 7:49am UTC](https://discourse.processing.org/t/a-javascript-prototype-question/24699 "2020-10-19T07:49:45Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![flamo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flamo/32/1576_2.png) [@flamo](https://discourse.processing.org/u/flamo)
#### Post date: [October 19, 2020, 7:49am UTC](https://discourse.processing.org/t/a-javascript-prototype-question/24699/1 "2020-10-19T07:49:45Z")

</div>

I am currently learning on how to use prototype in JS.  
I am curious about what happened after I extended a prototype and then I store the function as variable. why the console.log gives me a _Window_ object for the _canvas_? Why don’t I just get the empty function object i just created?

Here is the code:

```auto
var img = loadImage("/idle.png");
var s = img.splitter();
console.log(s);

p5.Image.prototype.splitter = () => {
  // some irrelevant codes here
  return this;
}
/** The output in short:
Window {close: function close(), stop: function stop(), focus: function focus(), blur: function blur(), open: function open()…}
*/

```

Thanks.

---

<div class="post-metadata">

### Author: ![flamo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flamo/32/1576_2.png) [@flamo](https://discourse.processing.org/u/flamo)
#### Post date: [October 19, 2020, 8:24am UTC](https://discourse.processing.org/t/a-javascript-prototype-question/24699/2 "2020-10-19T08:24:17Z")

</div>

Oh. I found an answer here:

> <https://stackoverflow.com/questions/80084/in-javascript-why-is-the-this-operator-inconsistent>

Never mind.

---

<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 19, 2020, 8:32am UTC](https://discourse.processing.org/t/a-javascript-prototype-question/24699/3 "2020-10-19T08:32:49Z")

</div>

> [@flamo](#):
>
> `p5.Image.prototype.splitter = () => {`

As you’ve already found out just now, creating a function via `=>` syntax binds its `this` permanently to its current context at creation time:

> **[Arrow function expressions - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)**
>
> An arrow function expression is a compact alternative to a traditional function expression, with some semantic differences and deliberate limitations in usage:

But method functions need to have their `this` to be the current object which had invoked it at call time:

`p5.Image.prototype.splitter = function () {`

---

<div class="post-metadata">

### Author: ![flamo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flamo/32/1576_2.png) [@flamo](https://discourse.processing.org/u/flamo)
#### Post date: [October 19, 2020, 8:37am UTC](https://discourse.processing.org/t/a-javascript-prototype-question/24699/4 "2020-10-19T08:37:14Z")

</div>

Good answer as always. Thanks. 😇

For the completeness of this thread:  
The answer I provide is outdated now. We don’t need `var that = this;` to let `this` refer to the object now. My mistake was to use the “=\>” instead of the function().
