Javascript major mode in Emacs - javascript

Which javascript major modes exist in Emacs, and what are their key-features ?

js2-mode: a new JavaScript mode for Emacs This is part of a larger
project, in progress, to permit writing Emacs extensions in JavaScript
instead of Emacs-Lisp.
Features: M-x customize Accurate syntax
highlighting Indentation Code folding
Comment and string filling Syntax errors Strict
warnings jsdoc highlighting
http://steve-yegge.blogspot.com/2008/03/js2-mode-new-javascript-mode-for-emacs.html
With some documentation.

I think what you want is this:
http://www.corybennett.org/download/javascript-mode.el
Then again, maybe this what you are looking for?
or this?
People seem to prefer (at least given the highest rated answer):
Updated: http://steve-yegge.blogspot.com/2008/03/js2-mode-new-javascript-mode-for-emacs.html

I use Steve Yegge's js2-mode, and like it a lot. It's quite configurable, its indentation ideas match mine, and most impressively it has a full JavaScript parser in it, so it can alert me to syntax errors as I type (indispensable for little things like trailing commas in property lists that bork IE).

Espresso mode is supposed to be quite good as well.

Related

Syntax folding in JavaScript files does not work in Vim

I've started using Vim 7.4 on Ubuntu and am very pleased with it but there is just one thing driving me crazy: code folding doesn't work (at least for JavaScript)!
The syntax is automatically set to JavaScript when a js file is opened and syntax highlight works so I don't get it. The foldmethod is initially set to "manual" and setting it to "syntax" doesn't make a difference, which puzzles me. I did add let javaScript_fold=1 to my .vimrc file.
Any clue? I'd be very grateful. Thanks!
It's tough to say the exact cause of this issue, but if you don't have a javascript.vim file you probably should. I suggest starting with this enhanced javascript syntax config. It is likely to fix your javascript folding issue, and much more.
If you just want to focus on the folding issue you might try creating your own javascript.vim file in ~/.vim/syntax/javascript.vim that contains code along the lines of what I have given below. You may want to adjust the fold level to your liking (0 is completely folded). However, this simple version will not play well with comments containing curly bracket characters, which is where you will want to go with a more robust javascript.vim like the one I have linked.
syntax region foldBraces start=/{/ end=/}/ transparent fold keepend extend
setlocal foldmethod=syntax
setlocal foldlevel=0
I should add that both myself and the other responder are suggesting that you need a javascript.vim, and in fact by some of the same contributors. However, the one I am suggesting was last updated in December of 2015 as opposed to 2009.
I don't know why your solution isn't working, but a possible solution is to use a user-created vimscript available at http://www.vim.org/scripts/script.php?script_id=1491
Just had this same issue answered on Vim Stack Exchange, and the answer is that if you do use the stock syntax/javascript.vim file, then you have to set
vv
let g:javaScript_fold = 1
^^
The difference between the command in the question and here is the g: part (highlighted above). I'm new to Vim scripting, but I believe the difference is that let javaScript_fold=1 sets a script-local variable, making it confined to your .vimrc file, and the example above makes it global (which seems to be confirmed by this Stackoverflow thread). See more on this at section 41.2 Variables in :help usr_41.txt and :help internal-variables.
This Reddit thread was also enlightening; it's not JavaScript-related but the folding seems to be useful for JS files as well.

Passing two parameters to Javascript function

Really really silly question, but I can't get this to work:
http://jsfiddle.net/t4R46/
function showOne(parentid, childid){
alert(parentid);
alert(childid);
}
<a href="#" onClick=showOne('div1', 'div2')>91</a>
Basically I want two parameters to be passed to the function but I get:
"Uncaught SyntaxError: Unexpected token }"
Is there any smart site to verify javascripts in? The error reporting in ie Chrome leaves a bit to be desired.
/Patrik
You need quotes around the attribute value:
91
<!-- here -----^ and here -----------^ -->
In HTML, an attribute value can only not have quotes if it doesn't have spaces (or a few other characters) in it. Since the value you want to set has a space in it, you need to put it in quotes.
Is there any smart site to verify javascripts in? The error reporting in ie Chrome leaves a bit to be desired.
You're looking for "lint" tools. Probably the most famous lint tool for JavaScript is Crockford's jslint. But it is heavily influenced by his personal style and opinions, so there's an active fork called jshint that's very popular. There are online and command-line versions of both. Some IDEs offer lint options as well, such as WebStorm from JetBrains.
Double quotes will help.
91
The best tools are : jshint, and jslint
You are missin "
91
FIDDLE

In JavaScript does /** have any special meaning?

I know I can add comments like so:
//This is a comment,
/*so is this*/
But when I do this
/**comment?*/
It has a different color in my text editor (notepad++) and I was wondering whether it has any special meaning, or if it is just a random feature of notepad++.
Here is what it looks like in the text editor:
No, it has not any special meaning. It's more common to use that syntax when documenting code via comments.
The Java language supports three kinds of comments:
/* text /The compiler ignores everything from / to */.
/** documentation /
This indicates a documentation comment (doc comment, for short). The compiler ignores this kind of comment, just like it ignores comments that use / and */. The JDK javadoc tool uses doc comments when preparing automatically generated documentation. For more information on javadoc, see the Java tool documentation.
// text
The compiler ignores everything from // to the end of the line.
Not in JavaScript itself, but some editors will treat it like a JSDoc (https://github.com/jsdoc3/jsdoc) comment to help with autocomplete, etc.
You can also run your code through something like JSDoc to automatically generate HTML documentation for your codebase.
Probably notepad++ identifies it with two different colors just to diversify the type of comment. for a programmer a comment may be more or less important than another :)
might seem like a silly feature, but it is not

Why does jQuery 1.4.2 compressed have a syntax error?

I always have noticed this, including in versions before as well. About half way through jQuery's compressed version you'll see some regex:
[^[\]]*\]|['"][^'"]*['"]|[^[\]'"]+)+\]|\\.|[^
The error appears to be at ['"]
I'm sure it's not really a syntax error, but all my code editors recognize it as one, which makes development a pain if I try to combine JavaScript files. Does anyone know what's going on here?
It's a character entity in a regular expression, as specified by the square brackets. There are no restrictions on quote characters in them. All that's going on is buggy syntax highlighting. Ask the developer of your editor.
your code editor sucks, this isnt a syntax error if its inside of a regex literal, which i suspect it is.
the code editor you use probably doesnt support regex literals properly, and that its a string , which would cause the error

JSLint reports "Unexpected dangling" character in an underscore prefixed variable name

I know that some people consider the presence of a leading underscore to imply that a variable is "private," that such privacy is a fiction, and assume this is why JSLint reports such names with an error message.
I use Google Analytics on a Web site I am building. I make reference to GA's variables, such as "_gaq."
I am trying to get my JS code to be 100% JSLint clean (I'm not religious about my coding style, and so will go with Mr. Crockford's counsel). That said, I can't do anything about Google's variables names... so, I guess I can't get 100% "clean."
I post here in case I've misunderstood the message, and can do something to comply with JSLint practices.
Ah, I've got this handled... I wrap the statements that use the underscore prefixed variables with JSLint commands to disable, then re-enable this class of error:
/*jslint nomen: true*/
... statement(s) with _var ...
/*jslint nomen: false*/
The best way to handle this is just to enable the "Tolerate dangling _ in identifiers" (nomen) option. See http://www.jslint.com/lint.html for details...
JSLint is just a code quality tool. Not completely passing its tests does not mean your code is bad; it simply means you don't follow all the conventions laid out by its creator. Although JSLint makes very good suggestions, it is not always possible to fulfill them all, especially when using someone else's library which was not tested against it. Rather than littering your source code with meaningless meta-comments, you should check your code with the "Disallow dangling _ in identifiers" option disabled, since it seems not to makes sense to use with your particular code.
I use JSLInt with node.js. You can pass --nomen flag to get around this feature
jslint --nomen myfile.js

Categories