[英]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] );
}
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.