Access calling object from Javascript method

Rawan Moukalled

In JavaScript, for an anonymous function defined inside the object, the this keyword refers to the object itself. However, when the function is defined outside the object scope, this refers to the window object, example:

function foo() {
  console.log(this)
}

let A = {
  att: foo(),
  att2: "bla"
}

A.foo();

[EDIT]: I made a mistake in the original code, I meant to call the function as A.att() (and I am obliged to keep att: foo(). My code is basically the following:

function foo() {
  console.log(this)
}

let A = {
  att: foo(),
  att2: "bla"
}

A.att();

The output here is object Window, and not the object that I want to access, which is A. And I can't change A, so how can I access A from foo()?

nashcheez

A function's this keyword behaves a little differently in JavaScript compared to other languages. It also has some differences between strict mode and non-strict mode. In most cases, the value of this is determined by how a function is called. It can't be set by assignment during execution, and it may be different each time the function is called.

To manipulate the this object by yourself you can use the JS methods call, apply & bind.

Lets demonstrate each with example:

Function.prototype.call()

Detailed info: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind

function foo() {
  console.log(this)
}

var A = {
  att: foo,
  att2: "bla"
}

foo.call(A);

Function.prototype.apply()

Detailed info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

function foo() {
  console.log(this)
}

var A = {
  att: foo,
  att2: "bla"
}

foo.apply(A);

Function.prototype.bind()

Detailed info: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind

function foo() {
  console.log(this)
}

var A = {
  att: foo,
  att2: "bla"
}

var x = foo.bind(A);
x();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling javascript Service/Method from array object

From Dev

calling an object method with a setTimeout function from within the same object in Javascript

From Dev

Calling JavaScript object method from QML ListView delegate

From Dev

JavaScript calling a method on an object syntax

From Dev

Javascript calling other object method

From Dev

Calling method from member object

From Dev

calling an object method from another method not working

From Dev

How to access base object from inside method Javascript

From Dev

Calling Javascript prototype method from another method

From Dev

(javascript) Calling a method on an object inside an array

From Dev

Access delegate object from a method

From Dev

Parse error from calling object method in ReactJS?

From Dev

Calling a method of a global object from a function

From Dev

Calling a Generic Method from BaseEntity on unknown object

From Dev

Calling prototype method from another object

From Dev

calling an abstract method from the subclass object

From Dev

Calling an object and its method from another object class method

From Dev

Calling an object's prototype method from another object's method

From Dev

Binding context when calling ES6 method. How to access object from within method called as callback?

From Dev

Access a Map object from a method to the build method

From Java

Calling a method on an Object from within a Class vs from within a method

From Dev

Calling external Javascript from Action Method

From Java

Calling Java method from JavaScript using ScriptEngine

From Dev

Calling a C# method from JavaScript

From Dev

MVC Calling ActionResult Method from JavaScript

From Dev

Calling a javascript function from java method in Cordova

From Dev

Calling requestAnimationFrame from inside a javascript object

From Dev

How to point in a method to the object you're calling the method from? (java)

From Dev

JavaScript expand object[method] to object.method for calling object.method()

Related Related

  1. 1

    Calling javascript Service/Method from array object

  2. 2

    calling an object method with a setTimeout function from within the same object in Javascript

  3. 3

    Calling JavaScript object method from QML ListView delegate

  4. 4

    JavaScript calling a method on an object syntax

  5. 5

    Javascript calling other object method

  6. 6

    Calling method from member object

  7. 7

    calling an object method from another method not working

  8. 8

    How to access base object from inside method Javascript

  9. 9

    Calling Javascript prototype method from another method

  10. 10

    (javascript) Calling a method on an object inside an array

  11. 11

    Access delegate object from a method

  12. 12

    Parse error from calling object method in ReactJS?

  13. 13

    Calling a method of a global object from a function

  14. 14

    Calling a Generic Method from BaseEntity on unknown object

  15. 15

    Calling prototype method from another object

  16. 16

    calling an abstract method from the subclass object

  17. 17

    Calling an object and its method from another object class method

  18. 18

    Calling an object's prototype method from another object's method

  19. 19

    Binding context when calling ES6 method. How to access object from within method called as callback?

  20. 20

    Access a Map object from a method to the build method

  21. 21

    Calling a method on an Object from within a Class vs from within a method

  22. 22

    Calling external Javascript from Action Method

  23. 23

    Calling Java method from JavaScript using ScriptEngine

  24. 24

    Calling a C# method from JavaScript

  25. 25

    MVC Calling ActionResult Method from JavaScript

  26. 26

    Calling a javascript function from java method in Cordova

  27. 27

    Calling requestAnimationFrame from inside a javascript object

  28. 28

    How to point in a method to the object you're calling the method from? (java)

  29. 29

    JavaScript expand object[method] to object.method for calling object.method()

HotTag

Archive