javascript - 访问 parent 的属性(property)

标签 javascript oop

您好,我需要更改 onMouseMove 的 mousemove 属性,但我无法访问 myfunc 对象,因为 this 引用了 el 不是父级!!

function myfunc (el) {
  this.el  = el;
  this.mousemove = false;

  el.onmousemove = function(){
      this.mousemove = true;
  };
}

最佳答案

只需存储对 this 的引用,您可以随意命名它。通常使用 thatself:

function myfunc(el) {
  var that;
  that = this;
  this.el = el;
  this.mousemove = false;
  el.mousemove = function () {
    that.mousemove = true;
  };
}

关于javascript - 访问 parent 的属性(property),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8071824/

相关文章:

javascript - Uncaught TypeError : not a function, 但函数存在

javascript - 用 python 服务器替换 Nodejs Socket.io 服务器

javascript - 如何创建使用前缀作为参数的自定义方法

oop - 当您单独设计对象模型时,您使用什么技术?

c# - 动态多态性 : Doubt

JavaScript : Check <img> is empty generate by for loop and if true do something

javascript - ASP.NET MVC 和 Mootools

javascript - HTML 文件中 javascript 的代码辅助在 Aptana 3 中无法正常工作

javascript - JS : OOP private functions/private fields

c# - 多态/覆盖