[英]How to create selector like jQuery with additional functions?
I am trying to understand how to work jQuery and other libraries. 我试图了解如何使用jQuery和其他库。 I would like to know how to create a selector with this format: 我想知道如何使用以下格式创建选择器:
$("#selector").get();
By the moment, I am trying the next, but i don't know how to run internal functions (get(), set()): 到目前为止,我正在尝试下一个,但我不知道如何运行内部函数(get(),set()):
var $ = (function() {
var jQuery = {
get: function() {
console.log("get() function!!");
return this;
},
set: function() {
console.log("set() function!!");
return this;
}
};
return function(el) {
return document.querySelector(el);
}
})();
I have read something about modular pattern design in JavaScript, but I don't understand all. 我已经阅读了一些关于JavaScript中模块化模式设计的内容,但我并不了解所有内容。
The way to make chainable functions, is to first and foremost create instances with the new
keyword. 制作可链接函数的方法首先是使用new
关键字创建实例。
This can be done "automatically" by making sure the this
value of the called function is an instance of itself, if not explicitly call it with new
. 这可以通过确保被调用函数的this
值是其自身的实例来“自动”完成,如果没有用new
显式调用它。
Then it's just a matter of returning the instance and using prototyped methods. 然后,这只是返回实例和使用原型方法的问题。
var $ = function(selector) { if (! (this instanceof $) ) { return new $(selector); } this.el = document.querySelectorAll(selector); return this; } $.prototype.css = function(prop, val) { this.el.forEach(function(element) { element.style[prop] = val; }); return this; } $('#test').css('color', 'red').css('font-size', '30px')
<div id="test">test</div>
const $ = function(selector) {
if (!(this instanceof $)) {
return new $(selector);
};
this.el = document.querySelectorAll(selector);
};
$.prototype.css = function(obj) {
this.el.forEach(function(element) {
element.style[Object.keys(obj)[0]] = Object.values(obj);
});
};
$.prototype.click = function(callback) {
this.el.forEach(function(element) {
element.addEventListener('click', callback, false);
});
};
jQuery this
or $(selector)
is array like [div, div]
not object {el: [div, div]}
, so its not using this.el
to modified the elements, here simplified version jQuery this
或$(selector)
是像[div, div]
这样的数组而不是对象{el: [div, div]}
,所以它不使用this.el
来修改元素,这里简化版本
if (window.$ === undefined) window.$ = (function () { var $, fun = {}, emptyArray = []; function Z(dom, selector) { var i, len = dom ? dom.length : 0; for (i = 0; i < len; i++) this[i] = dom[i]; this.length = len; this.selector = selector || ''; } fun.Z = function (dom, selector) {return new Z(dom, selector);}; fun.init = function (selector, context) { if (!selector) return fun.Z(); var dom = document.querySelectorAll(selector); return fun.Z(dom, selector); }; Z.prototype = { splice: emptyArray.splice, forEach: emptyArray.forEach, html: function (str) { return this.forEach(function (el) { el.innerHTML = str; }); }, css: function(obj, value){ if(typeof obj == 'object'){ // like: .css({background: 'red'}) for(var k in obj){ return this.forEach(function (el) { el.style[k] = obj[k]; }); } } else{ // called: .css('background', 'red') return this.forEach(function (el) { el.style[obj] = value; }); } } }; $ = function (sel, ctx) {return fun.init(sel, ctx); }; return $; })();
<div class="test"> AAAA </div> <div class="test"> BBBB </div> <button onclick="$('.test').css({background: 'red'})">red</button> <button onclick="$('.test').css('background', 'blue')">blue</button> <br /> <button onclick="console.log($('.test'))">log to console</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.