Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get [[boundthis]] from function

Tags:

javascript

I need your help.
I have 2 functions:

addMoveListeners: function(e) {
  e = e || window.event;
  // Binging context to function move
  moveListener = MYAPP.move.bind(e.target.parentElement);
  //
  if (e.target.classList.contains('move')){
    document.addEventListener('mousemove', moveListener, false);
    document.addEventListener('mouseup', MYAPP.removeListener, false);
  }
  resizeListener = MYAPP.resize.bind(e.target.parentElement);
  if (e.target.classList.contains('resize')){
    document.addEventListener('mousemove', resizeListener, false);
    document.addEventListener('mouseup', MYAPP.removeListener, false);
  }
  return false;
},

and this:

removeListener: function(e){
  e = e || window.event;
  //Here I want get element from function
  console.dir(resizeListener);
  // Function stores it in [[BoundThis]]
  document.removeEventListener('mousemove', resizeListener, false);
  document.removeEventListener('mouseup', MYAPP.removeListener, false);
  document.removeEventListener('mousemove', moveListener, false);
  document.removeEventListener('mouseup', MYAPP.moveListener, false);
},

How can I get property [[BoundThis]] from function resizeListener without execution.

like image 488
trunkovich Avatar asked Dec 10 '14 12:12

trunkovich


People also ask

How do I access BoundThis?

You cannot. [[BoundThis]] is an internal property of bound function objects. It is not programmatically accessible. You might be able to view it with inspection of the object via console, but to use it in your program logic you will need to write your own version of bind that exposes this value as a property.

What is .bind in JavaScript?

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

How do you bind an object in JavaScript?

We use the Bind() method to call a function with the this value, this keyword refers to the same object which is currently selected . In other words, bind() method allows us to easily set which object will be bound by the this keyword when a function or method is invoked.


1 Answers

You cannot. [[BoundThis]] is an internal property of bound function objects. It is not programmatically accessible.

You might be able to view it with inspection of the object via console, but to use it in your program logic you will need to write your own version of bind that exposes this value as a property.

like image 114
Bergi Avatar answered Sep 25 '22 02:09

Bergi