Javascript OOP回调";这";应用

Javascript OOP回调";这";应用,javascript,jquery,oop,Javascript,Jquery,Oop,我自己创建每个函数时遇到问题 我正在用面向对象的方法来实现它 基本上,我创建了自己的javascript库,模仿JQUERY习惯 检查此小提琴的动作: 孩子 孩子 JavaScript: "use strict"; (function(window) { var i, $, $Obj, ele; // global variable $ = function(el, length) { return new $Obj(el, length); // cr

我自己创建
每个
函数时遇到问题

我正在用面向对象的方法来实现它

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

检查此小提琴的动作:


孩子

孩子

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?

});
“严格使用”;
(功能(窗口){
变量i,$,$Obj,ele;//全局变量
$=函数(el,长度){
返回新的$Obj(el,长度);//创建新对象
};
$Obj=功能(el,长度){
ele=document.querySelectorAll(el);//获取选择器元素
this.length=ele.length;//计算ele的长度
对于(i=0;i
如何使那些
.child
样式颜色变为红色

我的代码怎么了

提前感谢…

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

还要注意的是,像
ele
i
这样的变量不是全局变量,它们被认为是使用它们的函数的局部变量

“严格使用”;
(功能(窗口){
var$,$Obj;//全局变量
$=函数(el,长度){
返回新的$Obj(el,长度);//创建新对象
};
$Obj=功能(el,长度){
var ele,i;
ele=document.querySelectorAll(el);//获取选择器元素
this.length=ele.length;//计算ele的长度
对于(i=0;i

孩子

孩子


变量i,ele;//全局变量
肯定会成为错误的来源。难以置信!非常感谢阿伦·约翰尼先生!只有一件事,你把VarI变成局部变量。所以,我必须一次又一次地重新定义它。如果我先在对象上定义它,会有什么问题?@ChingChing-应该像在ahh中那样打印0-14 4次!明白了!我现在明白了!非常感谢阿伦·约翰尼先生!
"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?

});