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
I have a JS script (widget) which is added to other websites. Technically its similar to Google analytics. My question is: what are your advices for ensuring robustness, keeping the code from interfering with other code on the site, avoiding errors for users etc. In general, what should I know to write a professional grade widget.
Notes:
I can't use any JS library such as jquery etc..
I am a big fan of Peter Michaux's guide on how he writes javascript widgets
Also useful are Christian Heilmann's script configuration and the module pattern
Those are generic javascript articles and aren't specific to a single library
Other useful tricks are things like wrapping your code in an anonymous function to stop it interfering with other global libraries.
(function() {
//Your code goes in here
})();
Regarding errors and best practice, John Resig has an interesting article on javascript strict that isn't in yet, but does have some handy information on the sort of things you should be avoiding.
If you're still coming to terms with scoping within your objects, then you might find this article on private and public variables useful as well a a bit more technical definition by Douglas Crockford
Finally, remember to run your completed code through a code quality tool
Related
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
Is there a standard way (or ways) of packaging a Javascript GUI control such that it is easy for others to use and evaluate?
For example - should classes be named a certain way, should certain methods always be implemented?
Is a raw Javascript GUI control easy to use, or should it have a wrapper to make it usable in a framework? Currently, the code is not written for any framework.
Some background: I am a long time C# (WinForms) developer. To learn Javascript/HTML5, I ported one of my C# GUI projects, a spiral-shaped slider/track-bar, to Javascript and implemented a test harness using a HTML page. My plan is to use JSDoc to generate documentation once the classes/methods are stable.
Thanks in advance for any guidance.
There are a few best practices:
Use as few dependencies as possible.
Dependencies increase the package size and add the possibility that someone up the chain will insert a vulnerability. One dependency may be including a dozen more.
Don't put things in the global scope.
You can't trust other modules not to collide with you. If you must, pick a unique name.
Set "use-strict".
Use strict forces the browser to use more precise interpretation of your code. It can reduce errors and boost performance.
Don't eval.
While not necessarily evil, it's dangerous and often is a shortcut to doing something right.
As with all best practices, there are cases to ignore each of these.
There are some common patterns that js elements use when attaching to a page. Most typical is for an element that most closely matches the behavior is added to the document with a class that the script recognizes. For example <input type="number" class="praise-helix"></input>. HTML5 also supports custom data attributes, meaning that this is valid <input type="number" data-helix></input>.
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 6 years ago.
Improve this question
What are the differences and similarities between QML and Javascript?
I am doing research on it, as I will probably give of a small presentation about QML soon. I have already looked at it on wikipedia, but I was hoping to get some answers from people with experience.
Note: I know some QML, I don't know any Javascript.
QML is a declarative language describing a tree of objects (in the QtQuick case a tree of visual items). The documentation has a fairly comprehensive documentation of the language. QML is used in the Qt framework only.
Javascript is an imperative language. Javascript is a very popular language used in many different places, for example embeddded in HTML sites or as part of node.js servers.
QML can actually include Javascript snippets, for example for bindings and signal handlers.
QML and Javascript serve very different purposes, so I don't see how to provide a list of similarities and differences.
Getting some downvotes there, my guess is that it is because you didn't research enough before coming here. And possibly because it doesn't sound like you are quite ready for a presentation on these languages.
But I'll see if I can at least point you in the right direction; QML is what is called a "markup language", not unlike XML, whereas Javascript is an "imperative language", like many others such as Python, C and Rust. More similar to Python, as it is also an "interpreted" language, whereas the others mentioned are "compiled" languages.
Hope it helps!
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
I was going to wait until the StackExchange for Joomla was approved before asking this question, but I'd really like to know the answer now!
According to http://docs.joomla.org/Adding_JavaScript there are 3 methods for incorporating javascript into Joomla using-
JDocument::addScriptDeclaration, JDocument::addScript and script.
I believe there is a fourth way which is perhaps easier than the methods listed and that is to include the javascript into the article directly. For example, in a Joomla article I could use the following line <script src="/javascript/jquery-1.7.min.js"></script>
Since this method isn't listed in the docs, I'm thinking it must be bad practice.
QUESTIONs
Is it bad practice to include javascript files directly into Joomla
via an article? If so, why?
What reasons (if any) are there for me to choose the first 3 methods
as opposed to the 4th? (I'm particularly interested to know if the 4th
method would affect cache or cause problems when using cdns.)
Including JS in an article is a bad idea because of loading order and dependencies.
Only do that if you need the script only on that single article page and if you've tested its behavior (and if you are no coder).
Best way to do it is via:
JHtml::_('script', 'path_to_script/script.js', false, true);
If you want to embed it right and not fiddle with PHP you could use something like this:
http://extensions.joomla.org/extensions/core-enhancements/performance/scripts/20245
If you need the script only on that one page, then it's better to add it in the article itself.
You can't use JDocument::addScriptDeclaration, JDocument::addScript because these are PHP functions. Unless your editor allows you to add PHP, then you can't use them.
The 4th option is the best.
The true answer lies in what you are trying to do with your script. There are many uses for Java script in articles, such as syndicated features or affiliate marketing.
TinyMCE limits some of the things you can use your articles for so I use JCE Content Editor for more power and options. There are free versions as well as pro versions available.
Check it out here: http://www.joomlacontenteditor.net/
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
I have always been taught with object-oriented paradigms when developing and rarely made use of JavaScript until about a year ago. I have taken many of my object-oriented skills with me which, for the most part, have served me will in JavaScript.
However, looking at some of the JavaScript libraries, some of their JavaScript is much, much more advanced. What I tend to do is, in a very C# manner, write the outline of my HTML page as follows:
<!DOCTYPE html>
<html>
<head>
<!-- some meta tags & import statements -->
<script>
function x() {
// do some stuff
}
function y() {
// do some more stuff
}
function z() {
// do some final stuff
}
</script>
</head>
<body>
<!-- some markup -->
<a onclick="x()">Call function x</a>
</body>
</html>
Which, as you'll see, is just some function declarations (obviously I have omitted a lot of detail to save time and unnecessary reading); but I want to take it to another level - but before I do, is there anything wrong with the above pattern?
Would I gain any benefit if I was to use self-executing functions which I understand are the correct way of writing JavaScript, and the cleanest way? If so, then I am confused by their use? I understand that they give developers a closed environment (where developers can split code into private/public functions); and stop developers adding to the 'window' variable.
Do I put my entire tags into a self-executing function? I understand it won't work immediately after putting it into a self-executing function, but I would like to at least try to make my code cleaner.
Two books I have (JavaScript: The Definitive Guide (Definitive Guides) by David Flanagan and Pro jQuery by Adam Freeman) are good at describing individual features of the language, but less so how to write entire libraries etc.
So if anyone could give any pointers, it would be much appreciated! Thanks.
One of the best JS books i've ever ready is Javascript Patterns by Stoyan Stefanov.
You can find the book here: http://shop.oreilly.com/product/9780596806767.do
I regularly re-read it. It will give you detailed information on tackling javascript from a more object orientated approach.
Another book I find myself going back to again and again is Javascript: The Good Parts by Douglas Crockford.
You can find that book here http://shop.oreilly.com/product/9780596517748.do
Self executing functions are more commonly used to handle scope issues, rather than design patterns.
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
I recently included Handlebars.js in a rails project, and a coworker balked at the notion. What are the realistic technical considerations when including an extra javascript library into a rails project?
Does the addition of an extraneous library significantly slow down the site delivery and user experience? Is this an example of engineering drama?
Has this been measured?
Adding additional libraries slows down the site delivery by several hundred milliseconds. It also requires some client time to parse and run its onload()-type functionality. From a human standpoint, it requires a bit of time to get used to using the new library. Depending on the level of complexity, usefulness, and time-saving of the library, this may be an acceptable tradeoff.
Handlebars is a great tool for templating, but you really need everybody on your team to be on board to use it. It's not very nice to simply introduce a brand new way of doing things without really discussing things. Handlebars is a big enough change to warrant at least a discussion, if not a vote.
If you were just wanting to put it there to see if it would work in the future, or maybe just convert over a page or two, then you should do that in a separate branch and do a quick prototype and demo for the team.
Depending on whether there is a valid business case and legitimate usefulness, you and the team can decide whether to convert your application to use it.