Strange behavior in a possible way to comment

Asked

Viewed 145 times

7

I have two functions

/########### CARREGAR - LANÇAMENTO ###########/
var carregar_parametros=function(){
    alert('asd');
}
/########### CADASTRAR - LANÇAMENTO ###########/
var cadastrar_parametro=function(){
    if($("#lancamento").val()!=''){
        UiLoading.show();
        $.post('/paginas/teste/cadastrar_parametro',{
            lancamento:$("#lancamento").val(),
            tipo:$("#tipo").val(),
            ativo:$("#lancamento_ativo").attr('checked')=='checked' ? '1' : '0'
            },function(){
                carregar_parametros();
                UiLoading.hide();
                $("#cancelar_parametro").click()
                UiAlert('Parâmetro cadastrado com sucesso!','Atenção');
        });
    }else{
        UiAlert('Por favor, preencha os campos nulos!','Atenção');
    }
}

when I do the job cadastrar_parametro with the comment /########### CADASTRAR - LANÇAMENTO ###########/ there is no mistake but when I do the function carregar_parametros with the same style of comment it gives error

Uncaught Syntaxerror: Unexpected token ILLEGAL

And when I test the extension firebug from Firefox he shows me this

Syntaxerror: illegal Character /########### REGISTER - RELEASE

#####/

What can it be?

  • 1

    JS comments should not be // for a line, /**/ for multi-line?

  • @Omni yes, but a while ago I tested with this kind of comment and it worked

  • 2

    @Silvioandorinha, if it worked for some time, I do not know, but this is not a standard form of comment in javascript. And I see no problem in using the common javascript comment: /*########### CARREGAR - LANÇAMENTO ###########*/, I don’t see why you want to invent =/.

  • perhaps the /## algo ##/ is interpreted as a regex

  • 1

    @Silvioandorinha when looking at the jsfiddle you can notice that the first / (before pressing parameter) is being considered as a character. What does not happen in the next /## (before registering parameter). Anyway I think it would be good practice to use the form of JS comments.

  • @Fernando is not irrelevant! This type of doubt is common to anyone when learning a new programming language.

  • @Fernando knowledge of language is never irrelevant and I bet you didn’t know it was possible to make comments this way

  • 3

    @The point is that there is no documentation to prove that the comments can be made in this way (at least I can not find). And as the question implies, doing so causes strange behavior.

  • 3

    @Silvioandorinha, this is not a form of javascript comment, as demonstrated in this link, which Andrey quoted. And that question seems to me a little bit forced, because that kind of comment is not necessary, and I believe you already knew that before you even asked here.

  • @Fernando is not necessary more is possible, maybe this can be even a javascript error and this is the first post that talks about this strange behavior, and is not decontextualized because the question is very clear.

  • 2

    @Silvioandorinha the way you write the comment is interpreted in the Javascript language as a regular expression as in the Guilhermebernal answer, this is not a standard comment your question is really decontextualized because the title is "Comment types" and what you proposed here as a type of comment does not exist and anyone who is "learning" javascript will know this when he finds this post he will just get confused with his type "Regex/Comment", this is not a mistake! is the same thing as you ask why an exe does not open in Lion or is not how it works

  • @Tuyoshivinicius and decontextualized says : Essa pergunta não parece ser sobre programação, dentro do escopo definido na central de ajuda. -- She really isn’t programming ??????

  • 2

    @Tuyoshi What is obvious to you is not obvious to anyone else. If you see a code that uses this syntax to write a comment (which is in passing works and is perfectly valid as long as the semicolons are in place), it is fair to ask how it happened. Especially if the person does not know regular expressions. This is a syntax that he has never seen before and behaves like a comment. By your logic, that question should also be closed, right? http://answall.com/questions/14788/a-sintaxe-tem-algum-significado-especial

Show 8 more comments

5 answers

15


Responding more directly:

A Javascript comment is written as follows:

// Isso é um comentário.
/* Ou isso também é */

You can find more details in any book or reference you want.

About specifically your code, you have it here:

/########### CARREGAR - LANÇAMENTO ###########/

This is the syntax for declaring a regular expression. /isso é uma regex/. This is an object that can be put into a variable, in the same way as a number or a string. It doesn’t do anything the way that code does nothing:

4

The error problem is in semicolons. Note:

var carregar_parametros = function() {/* ... */}

What you are doing is the same as by a value (the function) in a variable. Semantically it is the same as:

var a = 5

But you didn’t put semicolon at the end! This is a syntax error:

var a = 5
/regex/

For it is read thus:

var a = 5/regex/

Add a semicolon at the end of the resolve function statement. But honestly... use true comments!

  • Finally an explanatory answer to the problem, unlike the others who only gave me suggestions.

4

Just adding to comments that have already responded to Javascript comment formats, look for code writing standardization (coding standards), which makes it easy for you or anyone else to maintain the code.

See that:

//###### CADASTRAR LANÇAMENTO ####### //
var funcao = function () {};

It may seem like a good highlight in the code to separate the different functions, but a comment in the style "JAVADOC" can be much more useful and elegant, mainly by being widely used:

/**
 * Carrega a UI e efetua o cadastro de um lancamento.
 * @return void
 */
var funcao = function () {};

1

No Javascript use // for one line comments and /* (...) */ for comments of 2 or more lines.

View documentation.

1

You may be using / / for a single line and /* */ for multiple lines for comments on Javascript.

// Comentário de uma linha

/*
   Comentário com
   Múltiplas
   Linhas
*/

Comment Statements

Even no semicolon is required at the end of a function, I believe that if you used it you would avoid the error already explained in the @Guilherme Bernal’s reply.

/########### CARREGAR - LANÇAMENTO ###########/
var carregar_parametros=function() {}; // <--
/########### CADASTRAR - LANÇAMENTO ###########/
var cadastrar_parametro=function() {}

0

You should use the comments:

/* */ or //

And it’s not good to use / #### ... ### / because this is a regex. I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.