Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Commonmark migration
Source Link

It depends on the JavaScript engine that is used but according to the Mozilla reference your "this" actually references the parent object since you are returning an object.

As an object method

When a function is called as a method of an object, its this is set to the object the method is called on.

In the following example, when o.f() is invoked, inside the function this is bound to the o object.

var o = {
 prop: 37,
 f: function() {
 return this.prop;
 }
};
console.log(o.f()); // logs 37

It depends on the JavaScript engine that is used but according to the Mozilla reference your "this" actually references the parent object since you are returning an object.

As an object method

When a function is called as a method of an object, its this is set to the object the method is called on.

In the following example, when o.f() is invoked, inside the function this is bound to the o object.

var o = {
 prop: 37,
 f: function() {
 return this.prop;
 }
};
console.log(o.f()); // logs 37

It depends on the JavaScript engine that is used but according to the Mozilla reference your "this" actually references the parent object since you are returning an object.

As an object method

When a function is called as a method of an object, its this is set to the object the method is called on.

In the following example, when o.f() is invoked, inside the function this is bound to the o object.

var o = {
 prop: 37,
 f: function() {
 return this.prop;
 }
};
console.log(o.f()); // logs 37
Source Link
Adwaenyth
  • 2.1k
  • 1
  • 15
  • 24

It depends on the JavaScript engine that is used but according to the Mozilla reference your "this" actually references the parent object since you are returning an object.

As an object method

When a function is called as a method of an object, its this is set to the object the method is called on.

In the following example, when o.f() is invoked, inside the function this is bound to the o object.

var o = {
 prop: 37,
 f: function() {
 return this.prop;
 }
};
console.log(o.f()); // logs 37
default

AltStyle によって変換されたページ (->オリジナル) /