2
I do not know if I could be clear in the title, and so I will illustrate. In jQuery, we could do something like this to make all H2 green:
$('h2').css('color','green')
I’m trying to get rid of jQuery by creating generic function based on what else I do on my website. So I was turning that code above into something like:
// $$ is my selector, to avoid conflicts with jQuery, and is set to
function $$(selector){
return document.querySelectorAll(selector);
}
// setStyle would be a function that would use a foreach to go through all the items the selector found (foreach as it is in the function does not work because it needs to be "called" by the array)
function setStyle(styleName, value){
forEach(function(x){
x.style[styleName] = value;
})
}
// This is how I would like to make the change on my site, but I don’t see how to pass the selector items so that foreach can use
$$('h2').setStyle('color','green')
*** Remembering that if I pass the selector as a parameter of the setStyle function, I already solve this, as an example below. But it’s not the way I want to apply.
function setStyle(elements, styleName, value){
elements.forEach(function(x){
x.style[styleName] = value;
})
}
setStyle($$('h2'),'color','green')
I hope I have been able to exemplify clearly, and I am open to any suggested change in the code that will help me to carry out the action as the example I mentioned above.
I really think advising to use the modification of a prototype in Sopt is not a good idea. At least a warning. "Proton pollution" is not a good practice and should almost always be avoided, since it can bring some problems.
– Luiz Felipe