Alert was used before it was defined ошибка brackets

Подключаю внешний js файл,решил проверить и наткнулся на неприятную вещь.Работаю в редакторе Brackets.
Код HTML документа:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
    <link rel="stylesheet" href="style.css">
    <script scr="js/script.js"></script>
</head>
<body>
   
</body>
</html>

Код JS :
alert('Hello World');

И естественно алерт не работает,ломаю голову,не пойму в чем проблемма.
5a87383ec3238648862215.jpeg
Подскажите в чем проблемма,ведь она там,где ее сообсвтенно не должно быть.
Зарание спасибо!

document‘ was used before it was defined

You get this because the document variable has not been declared. You can tell JSLint that it should assume the code will run in a browser and therefore assume that things like document and window are pre-defined. You can do so with a JSLint comment at the top of your file:

/*jslint browser: true */

['form'] is better written in dot notation

Since you know the name of the property, there is no need to use square bracket notation:

document.forms.form.submit();

alert‘ was used before it was defined

You can use the devel option to prevent this error (you also use it to allow, for example, console.log calls):

/*jslint browser: true, devel: true */

Unexpected '​'

There is some invisible character after the closing brace. Just delete that character to remove this error.

As @vvks pointed out, devel: true is the way to go

Assume in development

devel

true if browser globals that are useful in development should be predefined, and if debugger > statements and TODO comments should be allowed. It adds the same globals as this directive:

/*global
alert, confirm, console, Debug, opera, prompt, WSH
*/

Be sure to turn this option off before going into production.

Closing as answered.

Форум JScourse

Загрузка…

History

This warning has existed in two forms across the three main linters. It was
introduced in the original version of JSLint and has remained in all three tools
ever since.

  • In JSLint the warning given is «‘{a}’ was used before it was defined»

  • In JSHint and ESLint the message has always been «‘{a}’ is not defined»

  • In JSHint and ESLint the message «‘{a}’ was used before it was defined» is
    issued under closely related circumstances

The situations that produce the warning have not changed despite changes to the
text of the warning itself.

When do I get this error?

The «‘{a}’ was used before it was defined» error (and the alternative «‘{a}’ is
not defined» error) is thrown when JSLint, JSHint and ESLint encounter an
identifier that has not been previously declared in a var statement or
function declaration
. Some very common examples of this error are those that
refer to native environment objects:

  • «‘document’ was used before it was defined»
  • «‘window’ was used before it was defined»
  • «‘alert’ was used before it was defined»
  • «‘console’ was used before it was defined»
  • «‘require’ was used before it was defined»

In the following example we attempt to set the value of the undeclared variable
x and then attempt to use some native browser environment objects:

x = 10;
alert("Errors...");
console.log("Errors everywhere");

In JSHint and ESLint the «‘{a}’ was used before it was defined» error (as
opposed to the «‘{a}’ is not defined» error) is raised when a reference to an
identifier occurs before the declaration
of that identifier. In the following
example we reference the variable a before we declare it:

/*jshint latedef: true */
function test() {
    "use strict";
    a = 1;
    var a;
    return a;
}

Why do I get this error?

This error is raised to highlight potentially dangerous code. Your code may
run without error, depending on the identifier in question, but is likely to
cause confusion to other developers and could in some cases cause a fatal error
that will prevent the rest of your script from executing.

The example above is valid JavaScript when not running in strict mode. It will
create a property of the global object (in the browser, the global object is
window) with the given identifier. If you had accidentally omitted the var
keyword, you could have ended up overwriting a variable declared in a parent
scope, causing unexpected behaviour. If it does run in strict mode, it will
generate a reference error as it’s illegal to assign a value to an undefined
variable under such conditions ([ES5 Annex C]es5-c]):

Assignment to an undeclared identifier or otherwise unresolvable reference
does not create a property in the global object. When a simple assignment
occurs within strict mode code, its LeftHandSide must not evaluate to an
unresolvable Reference. If it does a ReferenceError exception is thrown.

If you are referring to an identifier that has been declared elsewhere (in
another JavaScript file included in the page for example), you can tell JSLint,
JSHint and ESLint about it by using the global directive:

/*global someFunction */
var x = someFunction();

If you have mistakenly omitted a var keyword, you can fix this error by simply
adding it in. If you omitted the keyword on purpose (to allow access to a
variable from other scope for example), declare the variable in the top-most
scope in which it should be available:

/*global someFunction */
var x = 10;

If you understand the concept of hoisting and prefer to define functions after
they are used (perhaps at the end of a file) you can tell JSHint to allow that
specific use case by setting the latedef option to nofunc:

/*jshint latedef: nofunc */
doStuff();

function doStuff() {
  return 1;
}

In the case of environment-specific global identifiers (like window or
document in the browser or module in Node.js) there are a few JSLint/JSHint
options that you can set to let the linter know what environment the code is
expected to run in:

  • browser — JSLint, JSHint and ESLint. Defines global variables available in
    the browser.

  • devel — JSLint and JSHint. Defines global variables that are only used in
    a development environment (such as alert and console).

  • node — JSLint, JSHint and ESLint. Defines global variables available in
    Node.js.

  • couch — JSLint and JSHint. Defines global variables available in CouchDB.

  • rhino — JSLint and JSHint. Defines global variables available in Rhino.

  • phantom — JSHint only. Defines global variables available in PhantomJS.

  • shelljs — JSHint only. Defines global variables available in ShellJS

  • typed — JSHint only. Defines global typed array variables (such as
    Int32Array and ArrayBuffer)

  • wsh — JSHint only. Defines global variables available in Windows Script
    Host

  • mocha — ESLint only. Defines global variables available in the Mocha test
    framework

JSHint also has a set options that tell it about libraries your script has
access to. They can be set in the same way as the environment options listed
above:

  • dojo — Defines global variables provided by the Dojo Toolkit

  • jquery — Defines global variables provided by jQuery

  • mootools — Defines global variables provided by Mootools

  • prototypejs — Defines global variables provided by PrototypeJS

  • yui — Defines global variables provided by YUI

In JSHint 1.0.0 and above you have the ability to ignore any warning with a
special option syntax. The identifier of this warning is W117.
This means you can tell JSHint to not issue this warning with the /*jshint
-W117 */
directive.

In ESLint the rule that generates this warning is named no-use-before-define.
You can disable it by setting it to 0, or enable it by setting it to 1.

Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Roy Huang

I am learning J.S. using Brackets.

When I was trying to use console.log, it tells my there is a JSLint Problem, ‘console’ was used before it was defined.

I don’t know what could cause this happen and how to solve it, please help.

Thank you .

3 Answers

Matthew Orndoff December 20, 2014 4:38am

Console.log() is not actually defined in standard Javascript, so JSLint has no idea what you’re talking about – it thinks you’re trying to call a method «.log()» from an object «console» that wasn’t defined yet. However, when you run your script in the browser, you should get output in the DevTools console.

Hope that helps.

Roy Huang

Hello Matthew, thank you for your prompt and clear answer.

If I understand clearly, I don’ t have to worry about about the warning, and it will just display normally if I run it in my browser?

Justin Walker

The JSLint thinks that when I use the alert(«Your Text Here»); script, I am using a function. Please fix ASAP
All I did was put an alert(); script in the javascript file and I got an error.
I expected it to run an alert.
I get the error in JSLint: «‘alert’ was used before it was defined. alert(date);»
Brackets version: Latest
OS version: Windows 7 Premium
I’ve tested without extensions
No Console errors.
HELP

F Code Inspection (Linting)

picture
asportnoy

Most helpful comment

picture
vvks
on 23 Aug 2016

👍5

All 3 comments

vvks picture
vvks
on 23 Aug 2016

👍5

As @vvks pointed out, devel: true is the way to go

Assume in development

devel

true if browser globals that are useful in development should be predefined, and if debugger > statements and TODO comments should be allowed. It adds the same globals as this directive:

/*global
alert, confirm, console, Debug, opera, prompt, WSH
*/

Be sure to turn this option off before going into production.

Closing as answered.

petetnt picture
petetnt
on 23 Aug 2016

👍1

ahmedaniss25 picture
ahmedaniss25
on 13 Sep 2020

Was this page helpful?

0 / 5 — 0 ratings

Возможно, вам также будет интересно:

  • Alert irruption ошибка терминала что делать ingenico
  • Alert irruption ошибка терминала сбербанка
  • Alert irruption ошибка терминала перевод
  • Alert irruption ошибка терминала как исправить ошибку
  • Alert irruption ошибка терминала ingenico ict220

  • Понравилась статья? Поделить с друзьями:
    0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии