javascript guidance re arranging functions into files [closed] - javascript

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.

Related

Best way to remove unused javascript suggested by lighthouse. Is setTimeout a good idea? [closed]

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 1 year ago.
Improve this question
Lighthouse suggests that I need to remove the unused javascript from the third party javascript library say index.js . It is okay for this particular library to load after few 10 seconds once the page is loaded. So I used the SetTimeout method to do that but I don't think that is the right approach to do it.
Once I do that, lighthouse does not complain about unused javascript from this library and improves the score, but I am worried that in production Google measure the overall performance of the page from the moment page loads to the moment user leaves the page. So in that sense, the unused javascript is never removed and just delayed for the execution. People also suggested that lazy loading JS on user events will help but in our the case mentioned above, the JS should load automatically.
I am basically looking for the suggestion to:
How to handle the JS libraries that I can't get rid of and which has lot of unused code in the page?
Is setTimeout good solution for the above case?
Is my understanding of performance calculation by Google correct (in production environment) although it does not show unused JS after delaying for 10seconds?
I would be more than happy to get the answers.

Best way to use javascript in anchor tag [closed]

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 7 years ago.
Improve this question
There are many ways to use JavaScript. When I use JavaScript with an anchor, I write code like this and I think this way is right.
Method One
But my co-worker uses JS like this.
Method Two
Is there a coding standard or are both methods correct?
DISCLAIMER: Inline JavaScript is, generally speaking, a bad idea, and 99% of the time you're much better off separating concerns, and using a library, such as jQuery, or whatever similar toolset that your framework of choice recommends.
Nonetheless, to answer your question, if you must use inline JavaScript, I recommend that you omit the "JavaScript:" keyword. It specifies a "pseudo-protocol," and is not necessary for modern browsers to interpret the code. It is a relic from the last decade, and there is a bug with some versions of IE:
"There is one (somewhat obscure) bug with the javascript protocol - in
Internet Explorer*, it will think you are leaving the page when you
click the link. If you are using window.onbeforeunload, then your
navigate-away message will appear at this time. For this reason alone,
we've stopped using the javascript protocol completely so we don't
have this bug show up because we forgot to check for it when we add a
navigate-away message to some page."
When do I need to specify the JavaScript protocol?
https://bytes.com/topic/javascript/answers/504856-javascript-pseudo-protocol-event-handlers
Both the ways are ok but in first way you should use a external JS file. Otherwise it is ok.
For small tasks and events second ways is good.

Messy javascript as long as it works, does it matter? [closed]

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 7 years ago.
Improve this question
I have a question regarding the act of breaking the laws of javascript (Don't repeat code, etc) and that is, does it really matter as long as the performance and functionality isn't affected in anyway?
I have a script with 200+ line of code that looks messy after I did some work arounds, made it compatible with every browser and bugtested it a few times, and even after that it still runs pretty great even if the code isn't as organized. I haven't run into any errors with it yet and the load time is supreme. I should also note that I learned javascript a few weeks ago, so I am not perfect at it.
I do understand that if you're writing a huge applicaton it may have a noticeable impact on the performance and load time.
This has been on my mind for a few days and I just wanted to see your opinion about it, so please post a detailed opinion and maybe some tips and tricks as your answer. Thank you. :)
Yes, it matters. A lot.
You cite performance and download time as if they were the most important things.
I'd say that readable code that's easily understood by others might be more important than your measures.
Keeping code DRY, organized, neat, and readable are the signs of a professional in my view. Sloppy, copy & paste messes come from hacks.

How to Detect, Measures & Debug Javascript strain on CPU? [closed]

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 9 years ago.
Improve this question
This question is pretty simple:
How does one go about detecting and debugging javascript stress on the CPU and/or optimize the code?
As you start building more and more code you reach points where things slow down. How do you figure out if you have unnecessary js running, maybe redundant code, OR if it's getting too much for a computer? My computer is pretty powerful but I can't assume everyone has a monster computer.
Is there a function, program or some tools that can help with this task?
This question seems general but I really don't know how to simplify it otherwise.
Any help or pointing in the right direction is much apprecitated :) Thank you.
Try this app called Spy-JS.
It is a tool which allows you to trace your code, and figure out which executions are taking the longest, allowing you to figure out where you need to optimize.
The tool you are searching for is called a "profiler".
The Firebug extension for Firefox comes with such a profiler and allows you to measure which parts of your Javascript code take how much time to execute.
Keep in mind that it only measures the execution speed in Firefox. Other browsers might implement certain javascript features more or less efficient, which means that the performance bottlenecks could be in other places when your application is executed in another browser. But more often than not your own code is at fault when a program runs slower than it should.

What are some techniques to facilitate safe refactoring in Javascript? [closed]

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 8 years ago.
Improve this question
I'm coming from statically typed languages like C++ where there are tools (the compiler for one) that will remind me if I forget something obvious while refactoring. For example, if I change the number of arguments to a function but I forget to change all the calls to it. But it seems really easy to make this type of mistake in javascript. How do you avoid problems like this when refactoring javascript?
just like you do in c++/java. By writing tests.
Unit tests are the best. If you code is MVC, then you can certainly have unit tests with little effort, at least for the model layer. The benefit is you get feedback immediately. Check out QUnit
Functional tests via Selenium or equivalent are good too. They will find problems, but not immediately.
Only thing like that I can think of is: JSLint (http://jslint.com/)

Categories