I've creating a small library of JavaScript functions that I will be using. Some are for UI functions others are to return something.
I am wondering if this method is ok and not a bad move before going any further with this library. Also, please do not mention jQuery as this is all JavaScript!
HTMLElement.prototype.drag = function(bool) {
var self = this;
self.classList.add('draggable');
event.stopPropagation();
event.preventDefault();
var origLeft= parseInt(self.offsetLeft,10);
var origTop = parseInt(self.offsetTop,10);
var mdX = event.clientX;
var mdY = event.clientY;
var elements = [];
if(bool === true){
var j = window.localStorage.getItem('userSettings');
if(j){
self.style.left= j.left;
self.style.top = j.top;
}
}
function drag(bool) {
self.style.position="absolute";
self.style.zIndex=999;
self.style.left = origLeft + event.clientX - mdX + "px";
self.style.top = origTop + event.clientY - mdY + "px";
event.stopPropagation();
}
function stop(bool){
self.classList.remove('draggable');
self.style.zIndex=0;
document.removeEventListener('mousemove',drag,true);
document.removeEventListener('mouseup',stop,true);
if(bool === true){
var settings = {
left: self.style.left,
top: self.style.top
};
var b = JSON.stringify(settings);
window.localStorage.setItem('userSettings',b);
console.log(b);
}
event.stopPropagation();
}
document.addEventListener('mousemove',drag,true);
document.addEventListener('mouseup',stop,true);
};
The above code is of course for a draggable
function. Here is a getClientRects()
sort of function but better:
HTMLElement.prototype.getRect = function(){
var a = this.offsetLeft;
var b = this.offsetTop;
var c = this.offsetHeight + b;
var d = this.offsetWidth + a;
var f = this.offsetHeight;
var g = this.offsetWidth;
var obj = { };
obj.left = a;
obj.right=d;
obj.top=b;
obj.bottom=c;
obj.width = g;
obj.height=f;
return obj;
};
The purpose of showing you all this is to get your thoughts and maybe advise about prototype or should I not do it this way. I just like the method execution like this:
document.getElementById('div').getRect();
instead of doing it with a function getRect('div');
.
The problem is for one not sure what enumerable is as I've self taught everything, and never really read on the terminology. I know what one of the users means when he says iterating them using a for in loop though not sure how to make it non-enumerable. Nor how to go about this any better way as I don't want my users (who use jQuery a lot) though the library is huge and honestly I'd rather them use a vanilla JavaScript library in the sense that is minimized for purposes of what I am doing (making a huge plug-in with a library and other coding options they can have their hands on).
defineProperty
to add functionality. \$\endgroup\$