【问题标题】:what is this.e in javascriptjavascript中的this.e是什么
【发布时间】:2017-03-15 04:43:24
【问题描述】:

我正在学习制作像 jquery 这样的库,并试图理解一段代码,this.e 让我很难过,这个 'e' 来自哪里,他们正在为它分配对象,但是当他们返回这个时,他们返回只是这不是this.e。

function _(a) {
  var b = {
    key: "some val"
  };
  if (a) {
    if (window === this) {
      return new _(a)
    }
    this.e = document.getElementById(a);
    return this
  } else {
    return b
  }
}
_.prototype = {
    hide: function() {
      this.e.style.display = "none";
      return this
    }

HTML

<button onclick="_('abc')">click</button>
<div id="abc" style="width: 200px; height: 100px; background-color: pink;">   </div>

【问题讨论】:

  • this.e 来自this.e = document.getElementById(a);。如果那不能回答您的问题,我认为您需要提供更多详细信息,说明哪些部分让您感到困惑,EG。为什么你对this.e = X 感到困惑,却对_.prototype = Y 不感到困惑?
  • 因为原型在那里,那是内置的东西
  • 问题是什么是.e
  • 你应该研究动态编程语言。您可以在运行时将任何内容分配给对象。

标签: javascript javascript-objects


【解决方案1】:

当参数为事件时,使用 e 作为函数参数名称只是一个约定。就像你在这里的例子一样,事件是 onclick

【讨论】:

  • 在 OP 的示例中,e 似乎是一个元素,而不是一个事件
  • 这毫无意义! e 从不用作函数参数,e 不是事件!
猜你喜欢
  • 2012-02-19
  • 1970-01-01
  • 1970-01-01
  • 2014-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多