Skip to content

Instantly share code, notes, and snippets.

@mikoloism
Created February 12, 2022 14:43
Show Gist options
  • Save mikoloism/a1e28dc34bcf77bcc893e23be07aadd5 to your computer and use it in GitHub Desktop.
Save mikoloism/a1e28dc34bcf77bcc893e23be07aadd5 to your computer and use it in GitHub Desktop.
i'm find the oldest file in sub-deep folders in my PC and i'm interested it, self made jQuery Clone; wow!!
"use strict";
class fn
{
constructor(selector){
this.selector = this.select(selector);
}
// select
select(selector) {
if(typeof selector === 'string') return this.strSelector(selector.toString().trim());
else if(selector !== null && typeof selector === 'object') return this.objSelector(selector);
else return null;
}
// str Selector
strSelector(selectorText) {
if(selectorText.startsWith('.')) return this.selectClass(selectorText.substr(1));
else if(selectorText.startsWith('#')) return this.selectId(selectorText.substr(1));
else if(/^(_|[a-z]){1,}(_|-|[a-z0-9]){0,}$/i.test(selectorText)) return this.selectElm(selectorText);
else return null;
}
selectClass(classText) {
this.selector = document.getElementsByClassName(classText).forEach(e => e);
}
selectId(idText) {
return document.getElementById(idText);
}
selectElm(elmText) {
return document.getElementsByTagName(elmText);
}
// obj
objSelector(selector) {
return selector;
}
// text,html,append,val
html(htmlTag) {
if(/(''|""|``|null|undefined|NaN|false)/.test(htmlTag)) return null;
else {
this.selector.innerHTML = htmlTag;
}
}
static each(collection=this.selector, callback=(key,value) => {}) {
return callback(key=key+1, value=collection[key]);
}
// event
ready(callback) {
this.selector.addEventListener('readystatechange', callback, false);
}
on(eventName, callback) {
this.selector.addEventListener(eventName, callback);
}
click(arg1, arg2) {
if(typeof arg1 === 'function') {
// bind "this" as selector and just running arg1 as callback
this.selector.onclick = (e) => arg1;
}else{
// set selector with "select" and then run arg2 as callback
this.select(arg1).onclick = (e) => arg2;
}
}
}
function $(selector){
return new fn(selector);
}
$(document).ready(() => {
fn.each($('i'), (index, item) => {
console.log('text');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment