Strange behavior in a possible way of commenting

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 cadastrar_parametro function with the comment   /########### CADASTRAR - LANÇAMENTO ###########/ does not give any error but when I do the carregar_parametros function with the same comment style it gives error

  

Uncaught SyntaxError: Unexpected token ILLEGAL

And when I test Firefox's firebug extension it shows me this

  

SyntaxError: illegal character / ########### SIGNATURE - RELEASE

     ##### /

What can it be?

    
asked by anonymous 21.05.2014 / 14:55

5 answers

15

Responding more directly:

A comment in JavaScript 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 your code specifically, 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, just like a number or a string. This does nothing in the way that this code does nothing:

4

The error problem is in semicolons. Note:

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

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

var a = 5

But you did not put a semicolon at the end! This is a syntax error:

var a = 5
/regex/

Well read:

var a = 5/regex/

Add a semicolon at the end of the function declaration resolves. But honestly ... use real comments!

    
21.05.2014 / 15:23
4

Just adding comments that have already replied to JavaScript comment formats, look for coding standards, which makes it easy for you or anyone else to keep the code.

See:

//###### 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 "JAVADOC" style can be much more useful and elegant, mainly because it is widely used:

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

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

View the documentation .

    
21.05.2014 / 15:10
1

You may be using / / for a single line and /* */ for multiple lines for comments in JavaScript .

// Comentário de uma linha

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

Comment Statements

Even not having a semicolon at the end of a function, I believe that if you use it, you would avoid the error already explained in the response of @Guilherme Bernal.

/########### CARREGAR - LANÇAMENTO ###########/
var carregar_parametros=function() {}; // <--
/########### CADASTRAR - LANÇAMENTO ###########/
var cadastrar_parametro=function() {}
    
21.05.2014 / 15:12
0

You should use the comments:

/* */ or //

And it's not good to use / #### ... ### / because that's a regex. I hope I have helped.

    
22.05.2014 / 14:42