最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get [[boundthis]] from function - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Dec 10, 2014 at 13:37 trunkovich asked Dec 10, 2014 at 12:46 trunkovichtrunkovich 3272 silver badges10 bronze badges 3
  • 1 what is BoundThis, you mean this? – Prabhu Murthy Commented Dec 10, 2014 at 12:51
  • 1 console.dir(function) give me this - [[BoundThis]]: div.note.note2 – trunkovich Commented Dec 10, 2014 at 13:00
  • 3 @unikorn: the OP has called Function.prototype.bind on the handler -- I suspect the OP wants to get the this value that has been bound to the function. – Qantas 94 Heavy Commented Dec 10, 2014 at 13:08
Add a comment  | 

1 Answer 1

Reset to default 20

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.

发布评论

评论列表(0)

  1. 暂无评论