jQuery: how to write the double slash '//' so that it does not throw error? [closed]

StackOverflow https://stackoverflow.com/questions/14278451

  •  15-01-2022
  •  | 
  •  

Pergunta

I have this code:

(options.parent) ? top = options.parent.height / 2; : top = parent.height() / 2;

Because of the double slash that i use there it get's an error i know why i get the error i just don't know how to write it to work.
Thanks again.

Foi útil?

Solução

It has nothing to do with the "double slashes", remove the semicolon

(options.parent) ? top = options.parent.height / 2 
                 :top = parent.height() / 2;

Semicolon was meant to define end of statement(optional), the ternary operator is treated as a single statement.

(options.parent) ? top = options.parent.height / 2; : top = parent.height() / 2;
//                                                ^------ Wrong!

Outras dicas

I think it's not the slash problem but the semicolon. Try this:

top = (options.parent ? options.parent.height : parent.height()) / 2;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top