【问题标题】:Javascript OOP callback "this" applyJavascript OOP 回调“this”适用
【发布时间】:2016-07-01 23:08:06
【问题描述】:

在我自己的 EACH 函数上创建 callback 时遇到问题。

我正在使用 OOP 的方式来完成它。

基本上,我创建了自己的模仿 JQUERY 习惯的 javascript 库。

检查这个动作:https://jsfiddle.net/6pk068ep/1/

<div class="parent">
    <p class="child">child</p>
</div>
<div class="parent">
    <p class="child">child</p>
</div>

JavaScript:

"use strict";

(function(window) {

    var i, $, $Obj, ele; // global variable

    $ = function(el, length) {
        return new $Obj(el, length); // create new object
    };

    $Obj = function(el, length) {

      ele = document.querySelectorAll(el); // get selector element 

        this.length = ele.length; // count the length of ele

        for (i = 0; i < this.length; i++) {
            this[i] = ele[i]; // loop it
        }

    };

    $Obj.prototype = { // object prototype

       each : function(fn) { // create method each just like jquery

       var arr = []; // create array to store value

       for (i = 0; i < this.length; i++) {          
         arr.push(this[i]); // push it to arr variable
         }

       fn.apply(this, arr); // IS THIS THE CORRECT WAY TO APPLY IT? 
       return this; // return this, so, it's chainable for other method

      }

    };

window.$ = $; // make dollar sign global object

})(window);

$(".child").each(function() {

    console.log(this);
    this.style.color = "red"; // not working, what's wrong with my code? is it because my "apply" wrong?

});

如何将那些.child 样式颜色变为红色?

我的代码有什么问题?

提前谢谢...

【问题讨论】:

  • var i, ele; // global variable 肯定会成为错误的来源。

标签: javascript jquery oop


【解决方案1】:

当您说each() 时,假定将为集合中的每个项目调用回调,因此在这种情况下您需要在 for 循环中调用回调。

另请注意,elei 等变量不是全局变量,它们应该是您使用它们的函数的本地变量。

"use strict";

(function(window) {

  var $, $Obj; // global variable

  $ = function(el, length) {
    return new $Obj(el, length); // create new object
  };

  $Obj = function(el, length) {
    var ele, i;

    ele = document.querySelectorAll(el); // get selector element 

    this.length = ele.length; // count the length of ele

    for (i = 0; i < this.length; i++) {
      this[i] = ele[i]; // loop it
    }

  };

  $Obj.prototype = { // object prototype

    each: function(fn) { // create method each just like jquery
      var i;

      for (i = 0; i < this.length; i++) {
        fn.call(this[i], this[i], i);
      }

      return this; // return this, so, it's chainable for other method

    }

  };

  window.$ = $; // make dollar sign global object

})(window);

$(".child").each(function() {

  console.log(this);
  this.style.color = "red"; // not working, what's wrong with my code? is it because my "apply" wrong?

});
<div class="parent">
  <p class="child">child</p>
</div>
<div class="parent">
  <p class="child">child</p>
</div>

【讨论】:

  • 太棒了!非常感谢 Arun P Johny 先生!只是一件事,您将 var i 设为局部变量。所以,我必须一遍又一遍地重新定义它。如果我先在 Object 之上定义它,有什么问题?
  • @ChingChing jsfiddle.net/arunpjohny/ygfqez48/1 - 假设像jsfiddle.net/arunpjohny/ygfqez48/2一样打印0-14 4次
  • 啊!明白了!我现在明白了!非常感谢 Arun P Johny 先生!
猜你喜欢
  • 1970-01-01
  • 2012-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多