vendredi 26 décembre 2014

How to create jQuery like object functions with JavaScript


I'm working on trying to create a small library of helper functions for a project.


At the moment, I've setup a lot of functions in global scope that look like this:



function addClass(el, className) {
el.classList.add(className);
}

var target = document.querySelector('.target);
addClass(target, "added-class");


With adding a class as an example, I'd like to follow a jQuery like function so that it gets called like this:



var target = document.querySelector('.target);
target.addClass("added-class");


How can I setup a function so that it is called this way?





Aucun commentaire:

Enregistrer un commentaire