gpt4 book ai didi

javascript - 尝试实现类似于 jQuery 的东西

转载 作者:行者123 更新时间:2023-11-30 09:34:18 25 4
gpt4 key购买 nike

我正在尝试实现类似于 jQuery 的东西。
基本上,每当我要对 DOM 元素进行操作时,我都希望使用包装器对象的方法,而不是直接操作 DOM。

这是我目前的情况

HTML:

<a href='#!'></a>

JS:

var dd = function(selector)
{
return new dd.prototype.constructor(selector);
}
dd.prototype =
{
constructor: function(selector)
{
var nodes = document.querySelectorAll(selector);

for(var i = 0; i < nodes.length; i++)
this[i] = nodes[i];

this.length = nodes.length;
return Array.prototype.slice.call(this);
},
addClass: function(cl)
{
alert('a');
}
};

var a = dd('[href="#!"]');
a.addClass('asd');

问题:

  • 我在浏览器控制台中收到一条错误消息,指出 a.addClass 是
    不是函数
  • dd('a') instanceof dd === false$('a') instanceof $ === true,所以我知道我的逻辑是这里有点不对。

可悲的是,我不太明白自己做错了什么。

最佳答案

虽然重新发明轮子不是一个好主意,以下内容应该对您有所帮助。

考虑到 jQuery 支持自定义构建,所以,首先,看看这里https://github.com/jquery/jquery#how-to-build-your-own-jquery .

var dd = (function() {
function dd(selector) {
// class call check
if(!(this instanceof dd)) {
return new dd(selector);
}

Array
.from(document.querySelectorAll(selector), (el, i) => {
this[i] = el;
})
;

Object
.defineProperty(this, 'length', {
get: () => Object.keys(this).length
})
;
}

dd.prototype.forEach = function(cb) {
Object
.keys(this)
.forEach(i => {
cb(this[i], i)
})
;

return this;
}

dd.prototype.addClass = function() {
this.forEach(el => el.classList.add(...Array.from(arguments)));


return this;
}

dd.prototype.removeClass = function() {
this.forEach(el => el.classList.remove(...Array.from(arguments)));

return this;
}

return dd;
})();

var odd = dd('strong');
var even = dd('span');

window.setTimeout(() => odd.addClass('foo'), 2000);
window.setTimeout(() => even.addClass('baz'), 3000);

console.log('odd', odd.length);
console.log('even', even.length);
.cntr strong,
.cntr span {
display: inline-flex;
width: 20px;
height: 20px;
transition: 250ms all linear;
margin: 2px;
border: 1px solid cyan;
background: lightseagreen;
}

.cntr .foo { background: yellow; }
.cntr .baz { background: orange; }
<section class="cntr">
<strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span><strong></strong><span></span>
</section>

关于javascript - 尝试实现类似于 jQuery 的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44438855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com