TinyJS – Shorten JavaScript QuerySelect with $ and $$
21–30 of 94 posts
Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$
#22may I ask what's the point of `const tagNames =`? removing it would make no difference, as far as I can tell - just make sure the previous line ends with a semicolon (or add it at the same line as the opening square bracket)
Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$
#23This is the whole code: (() => { const assignDeep = (elm, props) => Object.entries(props).forEach(([key, value]) => typeof value === 'object' ? assignDeep(elm[key], value) : Object.assign(elm, {[key]: value})) const tagNames = ['a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', 'b', 'base', 'bdi', 'bdo', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'cite', 'code', 'col', 'colgroup', 'data', 'd…
window['$'] = selector => document.querySelector(selector) window['$$'] = selector => Array.from(document.querySelectorAll(selector)) is really what I want to have
createElement({
className: 'p-2 flex justify-end',
contents: createElement({
tag: 'img',
src: '/os-logo-maps.svg'
}),
});
I got the idea from Lea Verou's blissfuljs. When you're creating a bunch of nested elements it comes in handy.Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$
#24This is the whole code: (() => { const assignDeep = (elm, props) => Object.entries(props).forEach(([key, value]) => typeof value === 'object' ? assignDeep(elm[key], value) : Object.assign(elm, {[key]: value})) const tagNames = ['a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', 'b', 'base', 'bdi', 'bdo', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'cite', 'code', 'col', 'colgroup', 'data', 'd…
window['$'] = selector => document.querySelector(selector) window['$$'] = selector => Array.from(document.querySelectorAll(selector)) is really what I want to have
Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$
#25 const myDiv = div(
{id: 'container', className: 'my-class'},
h1('Hello World'),
p('This is a dynamically generated paragraph.')
);
document.body.appendChild(myDiv);
That's completely unnecessary these days with template strings. It's gonna be much faster as well to use browser's native parsing. const div = document.createElement('div');
let text = 'This is a dynamically generated paragraph';
div.innerHTML = `
Hello world
${text}
`;
document.body.append(...div.children);
Keep it simple!Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$
#26Like jQuery but for 2024?
Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$
#27` window.$ = document.querySelector.bind(document) window.$$ = document.querySelectorAll.bind(document) `
Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$
#28Why would this make any sense? const myDiv = div( {id: 'container', className: 'my-class'}, h1('Hello World'), p('This is a dynamically generated paragraph.') ); document.body.appendChild(myDiv); That's completely unnecessary these days with template strings. It's gonna be much faster as well to use browser's native parsing. const div = document.createElement('div'); let text = 'This is a dynamically generated paragr…
Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$
#29may I ask what's the point of `const tagNames =`? removing it would make no difference, as far as I can tell - just make sure the previous line ends with a semicolon (or add it at the same line as the opening square bracket)
Well, it's polluting the global window scope with all those helpers. That's a reason to never use this imo. There are plenty of other jquery-lite helper libraries out there. 95 percent the utility with hardly any cost in terms of script size. Converting querySelectorAll to an array is honestly most of the benefit of these libraries.
Now that NodeList is iterable, I don't even know that converting to an array is that useful anymore.
[...document.querySelector('div')] is pretty easy.
Iterator.from(document.querySelector('div')) too (doesn't work yet in Safari).
Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$
#30You could make it even smaller by removing all the element names, and just have it be passed in as an argument. Would also reduce the amount of functions you need to declare, though function count is likely not a problem for modern JS engines.
Anyway, great job you did there!