This is simply a training exercise. I have created a script for solving this using the resource
(function(window) {
function smp(selector) {
return new smpObj(selector);
}
function smpObj(selector) {
this.length = 0;
if (!selector ) {
this.el = [];
return this;
}
if (selector.nodeType ) {
this.el = selector;
this.length = 1;
return this;
} else if(selector[0]==='#'){
this.el = document.getElementById(selector.slice(1, selector.length));
this.length = 1;
return this;
} else if(selector[0]==='.'){
this.el = document.getElementsByClassName(selector.slice(1, selector.length));
this.lenghth=this.el.length
return this;
}
else return null;
}
})(window);
I then attempted to call it like this:
smp('body');
However, the browser was unable to find my smp definition.
In the future, I plan to add methods to use it in this way (for example, changing color):
smp('.myClass').method('attr')
I would greatly appreciate it if someone could point out my mistake.
Update: I am still facing issues with adding methods like:
smp('.myClass').method('attr')
I attempted to do it like this:
(function color(smp) {
window.smp.prototype.changeColor = function() {
for (var i = 0; i < this.length; i++)
this.el[i].style.color = color;
return this;
};
})(smp);