I'm trying to implement something similar to jQuery.
Basically, whenever I'd be doing something with DOM elements, I'd like to use a wrapper object's methods instead of manipulating the DOM directly.
This is what I have so far
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');
The problems:
- I get an error in the browser console saying that
a.addClass is not a function
dd('a') instanceof dd === false
while$('a') instanceof $ === true
, so I know that my logic is somehow wrong here.
Sadly, I don't quite get what I'm doing wrong.