Javascript developer from C# [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
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.

Related

using block scopes to organize complex code [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 3 years ago.
Improve this question
I am building a three.js library and found placing comments and nesting content below the comment in {} made it easy to navigate lots of code. I was wondering if there are other better ways to do this.
Why I would like to do this
the main reason: I can quickly collapse code based on the comments I see above the block scopes. Thereby making it easy to focus on the code I want to change.
I'd only use this in the class initialisation, or very complex methods. Or rather, any areas where there is significant code use with applicable labels for those areas.
I would never scope a section without an appropriate comment accompanying it.
This helps me scale these different sections, especially if they are not something that would scale into a new class or object within reason
Im aware a common problem with code bases is they can be extremely overwhelming, especially when learning them. I think this way of organising code could help make it more approachable and less overwhelming to look at
I would love if there was a feature in IDE's to do something like this
Example of its use:
Collapsed:
Un-collapsed:
Collapsed:
Un-collapsed
I also see this as a compliment to refactoring, whether it's abstracting into functions or more classes. This seems to be agile and deals with uncertainty in the code base especially if you need to neatly layer problems solutions into themselves in ways that would not qualify a new class, or new method.
I would say it's better to break down into separate functions. Adding unnecessary blocks could have surprising consequences for future maintainers. Many editors support using a special syntax within comments to aid code folding and navigation without altering the syntax tree.
[Edit]
Whilst I am really an Emacs-only guy (TM), the docs (https://code.visualstudio.com/docs/editor/codebasics) for Vscode suggest something along the lines of
//#region

QML vs 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 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!

Is there a guide about how to create beautiful HTML that is preped to be used with javascript? [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 9 years ago.
Improve this question
This might seem like an odd question, but I find that javascript is either easy or hard, depending on how you've coded the HTML. Is there a book or website that goes into detail about successful patterns and guidelines for coding HTML, so that it's very workable with jQuery, css and complex ajax applications? Like solid rules to live by.
Again, seems like a weird question maybe, but I don't know a better way to ask it. I just find myself always having to change the markup as new things come up - like switching between a hidden input element to a data attribute... or putting more ids or taking away ids - and I guess I arrive at the right way to do it, but I'm curious if someone has bothered to analyze this and came up with some great guidelines, standards and patterns so that the resultant HTML is right the first time.
Thanks
The first thing if you want to code some clean HTML that will be easy to work with is to make sure that your code is valid against an official DTD, HTML4 (here) or XHTML (here).
Then use id and class in a proper way (id only for unique section and class for repeatable ones) and name them correctly according to the context so they are easily reachable.
From my experience, I would actually suggest that, when it comes to large projects and professional JavaScript coding, the goal actually becomes to decouple the JavaScript code from whatever HTML it lives in.
As mentioned already, as long as you are using well formed HTML (DTD compliant), a library like jQuery shouldn't have any trouble operating on it. However, as best practice, I would recommend striving to isolate and encapsulate dependencies, whether they be because of HTML structure or just other chunks of JavaScript code.
the best way is to develop the html and javascript together. That way you can adjust the document structure to whatever you need.
This article seems to answer my question:
http://www.viget.com/inspire/extending-paul-irishs-comprehensive-dom-ready-execution/

Learning JavaScript by reading first or writing first? [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 8 years ago.
Improve this question
I did check (and read) a few similar questions here, but this really applies to everyone differently since no one learns the same.
My question is not on what resources to use, for that I have plenty.
I am working my way through JavaScript: The Definitive Guide (and I have the good parts as well).
I'm reading up on the core language but I don't feel like the information is sticking (or rather I'm not getting much out of it).
My question is should I just write an application in JavaScript and reference my books (I'm a very hands on person, but do enjoy reading), or should I keep reading the book, do the exercises, and then write a program?
In short: should I just write js app first and reference my book, or read the book first doing the examples and then write a js application? I've done some basic programming in PHP/Python (nothing meaningful yet).
I always learn math this way and I suppose, I'm conflicted in how I should tackle learning a language. I feel like I could read many books, but perhaps this isn't the best way for me (I know others learn differently).
For starts, I'm considering writing a simple calculator in JS.
Like for any languages I would suggest to do both.
You have to immediately start applying what you are studying. This way you will focus what you learned and understand better how the language works.
JavaScript is a highly flexible language. You can start soft, then learn new things each day and it will grow with you. You will never stop learning with JavaScript because it's easy to start but difficult to master.
You can start with really small scripts. When you start to feel confident you can create more advanced scripts, start using frameworks, write plugins for them, and ultimately your own library of functions.
I only suggest you to start learning best practices as soon as possible, because there are many things you have to avoid while programming in JS.
Then as a last word, always do what you feel like is better, don't go crazy trying to learn everything immediately. Everyone learns and works at his own pace.

Best practices for writing javascript widgets [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
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

Categories

Resources