Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I read alot of topics, went through alot of JS plugins on github, and alot of them have their own way of structuring code and which pattern to use. In some cases there is obvious why he went that way. But when dealing with smaller stuff, not big SAP, which pattern have most "pros" to go with?
For now, i am going this way:
https://github.com/goranefbl/GENS-Javascript-Boilerplate/blob/master/main.js
Yes, it works just fine, but i am wondering for smaller plugins, especially the ones that are just manipulating DOM (creating new elements/moving around/adding events listeners etc...), what would be best pattern to go with and of course why? Maybe I dont need to pollute namespace for small stuff.
Here is the example of latest component: gist.github.com/goranefbl/1b6144ee8cd8708c7511a1a2fb7c53a6
Its just creating unordered lists below select field, for easy styling.
Going with example boilerplate from above just doesnt feel right, ie it feels to much for something like this.
You're looking for a smaller pattern than the one you posted. Let's see what parts we can get rid of:
The defaults object and extendDefaults method aren't necessary, you can use
window.GENSPlugin = function(firstoption, secondoption) {
The question is, do you really need a plugin? You said you don't want to pollute the global namespace, so what about this:
(function() {
'use strict';
// not accessible from outside the function
var obj1 = document.getElementById("...");
// accesible from outside
window.obj2 = function() {
...
}
})();
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am currently working to implement multiple language support in a web app. Large parts of the website exist inside a canvas element, this means that auto-translate from browsers cannot translate it.
Currently I am working on a system where language variations are stored in an object like this:
export const TextContent = {
en: ['Hello', 'Goodbye'],
de: ['Hallo', 'Auf Wiedersehen'],
fr: ['Bonjour', 'Au revoir'],
}
The content for the website is then generated with code like this:
createElement('div', TextContent['en'][0]) or canvas.drawText(..., TextConent['en'][0])
However I don't think this is the best way to do it.
I have read other posts online were people recommend storing all of the text in JSON files like this:
content.en.json
{
"hello": "hello"
}
content.fr.json
{
"hello": "Bonjour"
}
This seems a bit cumbersome to me. I have also seen examples of people storing languages in key-value pair files. Such as
content.en.txt
hello=Hello
goodbye=Goodbye
content.fr.txt
hello=Bonjour
goodbye=Au revoir
Neither of those methods, or my own one, seem particularly good.
I am not using any framework like React or Vue.
What is the industry standard for this? Are there any good NPM packages for implementing this. I would really appreciate some advice. Thank you.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
In Javascript, is it acceptable to rename built in functions. For example, in my code, I use document.querySelector() tons of times. This is extremely long and tedious to type every single time (even with autocomplete from the IDE's side). Therefore, I decided to create a new function with a shorter name like the following:
let qs = selector => document.querySelector(selector);
Is this an acceptable practice in the JS community? Is this considered bad code? If so, why? Additionally, if doing this is alright, what are some drawbacks?
Thanks.
No.
Someone is going to come behind you to edit your code.
They will then have to track down your renaming function to actually see what it does.
Create an snippet in your IDE if it’s that much of an issue for you.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I just found This and it has some comparation with JQuery and others...
Its really faster than the others.
What you guys think about this. Is it ok to use only this? Is there anything that you will HAVE/NEED to use JQuery ?
Also, when it comes to performance. Is there a big difference between:
var test = document.getElementById('test-table');
test.attr('id','123');
var test = document.getElementById('test-table');
test.dataset.id = '123';
It doesn't matter since you can always wrap any DOM element around a jQuery object if you must.
var test = document.getElementById('test-table');
// Do some vanilla stuff
var jTest = $(test);
// Do some jQuery
The jQuery library builds upon the DOM API that is available to JavaScript. The only reason you'd need jQuery is to do a complex task that requires more effort in vanilla. In terms of performance, the difference is negligible. jQuery adds checks to be cross-browser compatible. If you code to modern standards, these checks are not necessary.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
From an experienced developer's perspective, is bad practice to create a web application using multiple JS frameworks ?
For example, if you start using AngularJS and if some tasks of the project can be done easier with JQuery, should you go for it, or try to make that part in Angular too ?
In my opinion, a framework should only be used if it's absolutly necessary. E.g if you do lots of DOM work, jQuery is the right one.
But, if you need a mvn framework, go for angular/backbone or something like this.
Tio many people today think that for each and every single problem, a framework is the best solution.
Sometimes, it could be the best solution to use 2 frameworks. E.g. jQuery + lodash.
So the answer is... It depends on the type of application you want to develop. My approach would be to allways ask whether the framework is really needed, or if you maybe just need a single function that you better could write on your own.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Have written a large system in ColdFusion/Javascript/MySQL. There is lots of Javascript. The few functions which are long are separated into cases. Currently I have all the js in 3 files, depending on the section of the application to which they refer.
Every now and again, some function which tested okay before turns out not to be working (I might have changed something elsewhere to cause that problem, without realizing I had to retest).
Okay, I deserve that, but when one js function in the file stops working, sometimes others do as well. So unexpectedly something I was counting on to validate things, or prevent a submit in the presence of errors does not work.
I've thought of breaking up the js so that any javascript function which is used by just one program will be resident there, and I would use the files with several js functions only for those that several programs have to access. That would insulate my functions somewhat from problems occurring elsewhere.
Is this approach recommended? Could someone offer good reasons for or against it? Or is there a way to "firewall" my functions so that problems with one do not spill over into another?
I'm feeling that the js is a weak link in my system -- that I can't trust it; yet I need it, so I have to find a way to make things more stable. Any and all suggestions to help would be much appreciated.
The suggestion "get better at Javascript" has already occurred to me. I am trying. Meanwhile, my needs are not very demanding. All my functions do simple things and are written in a pretty straightforward way. Yet I am having all this trouble.