简体   繁体   English

Javascript缓存选择器功能

[英]Javascript Cache Selector Function

I'm having problem for getting selector being cache, so, basically I'm not using JQUERY Framework. 我遇到了让选择器成为缓存的问题,所以,基本上我没有使用JQUERY Framework。 I create my own Framework that mimicry JQUERY pattern. 我创建了自己的模仿JQUERY模式的框架。

This is my code : 这是我的代码:

"use strict"

var $, i;

(function() {

    $ = function(el) {
        return new obj$(el);
    };

    var obj$ = function(el) {
       var cl   = document.getElementsByClassName(el),
           loop = cl.length;

       this.length = loop;

       for (i = 0; i < loop; i++) {
           this[i] = cl[i];
       }

   };

   obj$.prototype = {

       find : function(el) {

           if (this.length == 1) {
               this[0] = this[0].getElementsByClassName(el)[0]; // this is the problem, it's reset everything
               return this;
           }

       },
       css : function(obj, data) {

           if (this.length == 1) {
               this[0].style[obj] = data;
               return this;
           }

        },
        cache : function() {

            if (this[0].hasOwnProperty("find")) {
                return this[0]; // reset the selector from $() that has been hook by find function
             }
             else {
                return this[0].find(); // do nothing if $() has not been hook by find function
             }

        }

    };


})();

var parent = $("parent"), // i want this selector being cache
    child  = parent.find("child"); // but, when i hook this "find" function it's reset parent selector

child.css("color", "orange");
parent.css("color", "purple");

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

jsfiddle : https://jsfiddle.net/k6j70f1h/ jsfiddle: https ://jsfiddle.net/k6j70f1h/

The output was : child with color purple, but, why not parent also become purple? 输出是:紫色的孩子,但是,为什么父母也变成紫色?

I know that in my find function, i use this[0] = this[0].getElementsByClassName(el)[0]; 我知道在我的find函数中,我使用了这个[0] = this [0] .getElementsByClassName(el)[0];

So, It's reset the $() object selector. 所以,它重置了$()对象选择器。

How to prevent this problem occure? 如何防止这个问题发生? I just look at hasOwnProperty method. 我只看一下hasOwnProperty方法。 is it possible creating another function for checking hasOwnProperty? 是否有可能创建另一个函数来检查hasOwnProperty?

I want $() object keep its selector even has been chaining with find function? 我想$()对象保持其选择器甚至已经与查找功能链接?

Any suggestion guys? 有什么建议吗? thanks.. 谢谢..

The find method should return a new instance of the $ object, not modify the existing result. find方法应返回$ object的新实例,而不是修改现有结果。 Your implementation should also, like jQuery, accept a second argument which is the context in which the search is performed (defaults to the document ). 您的实现也应该像jQuery一样接受第二个参数,该参数是执行搜索的上下文(默认为document )。

So your find could be implemented like this: 所以你的find可以像这样实现:

find : function(el){
    return $( el, this[0] );
}

updated fiddle demo 更新小提琴演示

var $, i;

(function() {

$ = function(el, context) {
      context = context || document;
      return new obj$(el, context);
};

var obj$ = function(el, context) {
   var cl   = context.getElementsByClassName(el),
       loop = cl.length;

   this.length = loop;

   for (i = 0; i < loop; i++) {
       this[i] = cl[i];
   }
};

obj$.prototype = {

   find : function(el) {

       if (this.length == 1) {
           return $( el, this[0] );
       }

   },
   css : function(obj, data) {
       if (this.length == 1) {
           this[0].style[obj] = data;
           return this;
       }
    }
};
})();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将选择器传递给 javascript 函数 - Pass selector into javascript function 如果多次调用一个选择器,是否在该函数中缓存的性能更高? - Is it more performant to cache a selector in a function if that function's called multiple times? Javascript-$(document).on(“ click”,“ selector”,function…)之间的区别; 和$(“ selector”)。on(“ click”,函数…); - Javascript - Difference between $(document).on(“click”, “selector”, function …); and $(“selector”).on(“click”, function …); 是否可以将选择器作为函数参数传递给Javascript? - Is is possible to pass a selector as a function parameter in Javascript? 通过CSS Selector将参数传递给javascript函数 - Passing a parameter to javascript function via CSS Selector 在javascript函数中使用选择器“$(this).find()”jQuery - Using a selector “$(this).find()” jQuery inside a javascript function jQuery 选择器回调 function 中的全局变量 Javascript - Global Javascript vars in jQuery selector callback function Javascript&gt;解复用/缓存Promise函数调用 - Javascript > Demultiplex/cache a Promise function call JavaScript function 参数如何在 function 主体中用作 ZF590B432C30BE28D3C 选择器? - How can a JavaScript function parameter be used in the function body as a jQuery selector? JavaScript中的选择器 - selector in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM