Error javascript: In Strict mode code, functions can only be declared at top level or immediately Within Another Function

Asked

Viewed 46 times

0

While testing my application on an android 4.4, appears this error, however on android 5 the error does not appear.

Can anyone tell me what it might be.

1 answer

0


Following Vitor’s suggestion in the comment my question, I was able to identify and solve the problem, I decided to write this answer to help someone who might have the same problem.

The Problem

In some algorithms I would create a function within a condition or loop, that until the es5 generates error, in the es6 the error does not occur. Starting from Android 5, webview now implements es6, so the error no longer occurs.

Example:

function getNome(){
   if (nome !== null){

      return nome();

      function nome(){
         return _nome;
      } 
   }
}

Solution

In order for the problem not to occur, we must withdraw the statement of Function name () from within the if.

function getNome(){
   if (nome !== null){
      return nome();
   }

   function nome(){
      return _nome;
   }
}

That’s how it works perfectly.

Browser other questions tagged

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