by batat · 9y ago · 0 comments
export function extend(src) {
var obj, args = arguments
for (let i = 1; i < args.length; ++i) {
if (obj = args[i]) {
for (let key in obj) {
// check if this property of the source object could be overridden
if (isWritable(src, key))
src[key] = obj[key]
}
}
}
}
or just add super-unique stuff to our classes [2] DragSelect.prototype.removeClass = function( element, classname ) {
var cn = element.className;
var rxp = new RegExp( classname + \'\\b\', \'g\' );
cn = cn.replace( rxp, \'\' );
element.className = cn;
return element;
};
DragSelect.prototype.hasClass = function( element, classname ) {
var cn = element.className;
if( cn.indexOf( classname ) > -1 ) { return true; }
else { return false; }
};
or write tons of boilerplate code like if (typeof element === 'string') {
owner.element = document.querySelector(element);
} else {
owner.element = ((typeof element.length !== 'undefined') && element.length > 0) ? element[0] : element;
}
(mmm, it's so vanilla)[1] https://github.com/oncode/handorgel/blob/master/src/helpers....
[2] https://github.com/ThibaultJanBeyer/DragSelect/blob/master/s...
Object.assign(target, object1, objectN);
The second: element.classList.add(className);
element.classList.contains(className);
And the third has been fixed at a community level, as we've learned the value of API strictness, obviating such checks.I don't think anybody was against jQuery back when the alternative was helper functions, I certainly wasn't. But that's five years ago at this point. Modern JavaScript looks nothing like the code you posted, it's actually been built up into a pretty good language.
At this point in front-end development something like Babel is a must. If you aren't using Babel/Flow/Typescript in your workflow, you are putting yourself at disadvantage for no good reason.
For better or worse, transpiling is a fact of life.
I find this debate so aggravating. I'm glad you can do some of the stuff jQuery does using modern vanilla js. Perhaps jQuery is overkill for doing one hide/show (or toggle) action on the page. But what about the more complex stuff. What is primary motivation behind this nagging issue, of the load time of jQuery?
I suspect it is that many developers have some obsession over shaving a few milliseconds on their load time. For jQuery? One library? Is it that bad that you need to write vanilla js? So you can save a few milliseconds?
Can you have a sophisticated selector for Event Delegation without using jQuery but make the code concise and easy to read? What about child selectors ("ul.thisClass > li.thatClass" > span.otherClass").
I like jQuery because the syntax makes sense, its clear and concise, you can do complex things with less code. Maybe some of the new stuff can be clean and concise and easy to read, but all of it? I have my doubts.
Can you give us some examples of things that you're worried can't be written clearly using plain JS? Obviously everything you can do with jquery you can do without jquery. Its just a library.
> Can you have a sophisticated selector for Event Delegation without using jQuery but make the code concise and easy to read?
What do you mean by event delegation in this context?
> What about child selectors ("ul.thisClass > li.thatClass" > span.otherClass")
document.querySelector / querySelectorAll supports this. Its available in all browsers on all platforms if thats how you chose to make web apps: http://caniuse.com/#feat=queryselector
The not() selector for jquery is one. The closest() is another that I have used multiple times.
> What do you mean by event delegation in this context?
This is what event delegation looks like in jQuery; $("ul#listid").( "click", "li.classThatMatters", function() { //do something });
How would event delegation look when selecting a child element with a specific class? Event delegation from scratch in vanilla js is longer than this.
I don't know much about query selectors. I'm going to read up on them. I suspect it handles most if not all the basic stuff jQuery does. However I suspect that are use cases that it might now.
Native selectors are nearly as powerful. Your example works as-is, "not" is there, full reference at https://www.w3.org/TR/css3-selectors/#selectors.
Event delegation isn't native, but is now possible in five simple lines. That's the pattern: even when there's no native equivalent, new language features facilitate shorter, simpler implementations.
jQuery's performance impact is orders of magnitude worse than "a few milliseconds". On semi-old laptops and on mobile I've seen 200ms in just the time it takes to execute jQuery after loading it. There's also runtime overhead. It's not unbearable, but it's a real hit on a page with high user experience goals.
Native alternatives are often better–thought-out and better-integrated. For example, native Promises have better exception handling (thrown errors can be caught to continue down the "then" chain), more reliable execution order ("then" always defers callbacks, jQuery sometimes executes them inline), and integrate with `await`, like `const data = (await fetch("data.json")).json();`. (Though you should still transpile that, some browsers that don't support it are still in use.)
Besides that, unnecessary abstraction resists novelty and efficiency in the use of underlying resources.
For example, I don't support browsers without classList, so I don't need your regexp stuff. Maybe you do, but my code shouldn't be larger because of that.
Having said that, it's all about tradeoffs. Whether your project needs something as big as <insert JS framework or lib here> or if you can live with just needing a few utility functions, you make those choices, doesn't mean one is better than the other as it all depends on context and situation.
jQuery has become the defacto must-use library for javascript now which IMO is unfortunate, as a lot of sites and apps include it when they barely use most of what jQuery offers. I'm guessing most use jQuery for only a few things - Ajax, DOM querying, and events. I hope that sometime in the future jQuery would be more modular.
I haven’t done serious web work in ages but if I did I’m quite sure I’d find all of today’s “required” build tools quite frustrating.
See:
- https://github.com/webpack/webpack/issues/2867
- https://github.com/webpack/webpack/issues/4784
(Note: webpack doesn't do any dead code elimination, it relies on UglifyJS to do so)
Are you aware of any alternative that can do tree shaking? Reading those issues' comments there is a link to babel-webpack-tree-shaking [1] which seems to do it?
[1] https://github.com/Aladdin-ADD/babel-webpack-tree-shaking
Imagine your app uses, modals, dropdowns, multiselects, search autocompletes, sliders, rich text areas etc. Instead of having one cached utility library upfront you have many hidden utility libraries scattered around each component, which makes it difficult to say clearly that jQuery is always larger.
The other issue is maintainability. When you're working with third party components, hot-fixing an OS library can be fairly daunting if you need to also learn their own internal class structure, selector helpers, dom manip functions. Doing this in jQuery, it's fairly easy to fork the codebase and plop in a .clone() or .append() to fix a stale issue.
Ultimately mutating the DOM directly is just an awful job - it's solving problems that are ancillary to the problem that you are trying to solve (making great UIs). React and Vue feel like sensible abstractions, although there's still a far better but undiscovered solution to this problem.