In order to create a simple framework with jQuery style, I have written the following code:
(function(window) {
window.smp=function smpSelector(selector) {
return new smpObj(selector);
}
function smpObj(selector) {
this.length = 0;
if (!selector ) {
this.el = [];
return this;
}
if(typeof selector == 'string'){
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.length = this.el.length;
return this;
} else {
this.el = document.getElementsByTagName(selector);
this.length = this.el.length;
return this;
}
}
else return null;
}
window.smp.changeColor=function smpColor(color) {
for (var i = 0; i < this.length; i++)
this.el[i].style.color = color;
}
})(window);
It is currently functioning properly. I can make a call like this:
smp('div')
However, when I attempted to add the following method:
window.smp.changeColor=function smpColor(color) {
for (var i = 0; i < this.length; i++)
this.el[i].style.color = color;
}
It is not working as expected.
smp('div').changeColor('color')
(I am unable to call it in this manner)
I would appreciate any guidance on where I may be going wrong.
This code was influenced by an article that I read.