简体   繁体   English

括号中的JSLint表示未定义音频,但不抱怨图像

[英]JSLint in Brackets says Audio is not defined, but doesn't complain about Image

I have this code: 我有以下代码:

var me = {
    PreloadImage: function(src) {
        var e = new Image();
        e.src = src;
    },
    CreateAudio : function(src) {
        var c = new Audio(src);
        c.play();
    }
}

I have a problem with new Audio() , as JSLint in Brackets says that Audio has not been defined, but it doesn't say the same for Image : 我对new Audio()有问题,因为JSLint中的JSLint表示尚未定义Audio ,但是Image却没有这样说:

在此处输入图片说明

Sometimes I have to do things like window.console.log instead of console.log because console.log was not defined, but if that's the case, what do I have to add before Audio and Image ? 有时我必须做诸如window.console.log之类的事情而不是console.log类的事情,因为未定义console.log ,但是如果是这样的话,在AudioImage之前我必须添加什么?

Instead of var c = new Audio(src); 代替var c = new Audio(src); use var c = document.createElement('audio'); c.src=src; c.play(); 使用var c = document.createElement('audio'); c.src=src; c.play(); var c = document.createElement('audio'); c.src=src; c.play();

The accepted answer doesn't look like the best way to address that issue to me. 接受的答案似乎不是解决我这个问题的最佳方法。 You should either change your JSLint options or disable that rule on that specific line. 您应该更改JSLint选项或在该特定行上禁用该规则。

👉 Add it as a global 👉将其添加为全局

In both JSLint and ESLint , you can fix it either adding it as a global: JSLintESLint ,都可以修复它,也可以将其添加为全局ESLint

{
    "globals": {
        "Audio": true
    }
}

👉 Add the browser environment 👉添加浏览器环境

ESLint: : ESLint: ::

{
   "env": {
       "browser": true
   }
}

JSLint:

{
    "browser": true
}

👉 JSLint options in Brackets Bra括号中的JSLint选项

You can also update them in Brackets' preferences: 您也可以在Brackets的偏好设置中更新它们:

"jslint.options": {
    "browser": true,

     OR

    "globals": {
        "Audio": true
    }
}

👉 Disable linting or ignore that rule on that line 👉禁止掉线或忽略该规则

You can also disable linting for that line or for that specific rule on that line: 您还可以为该行或该行上的特定规则禁用掉毛:

ESLint : ESLint

const c = new Audio(src); // eslint-disable-line

Or: 要么:

// eslint-disable-next-line
const c = new Audio(src);

JSLint : JSLint

const c = new Audio(src); // jslint ignore:line

You have to wait for the DOM to be ready. 您必须等待DOM准备就绪。 Since your are using jQuery, please encapsulate your code in that: 由于您使用的是jQuery,因此请在其中封装代码:

$(document).ready(function () {
  // Your code...
});

You can also use this syntax: 您还可以使用以下语法:

$(function () {
  // Your code...
});

(Bonus tip: use the switch instruction in your code. RoNBeta.js is a bit scary...) (奖金提示:在代码中使用switch指令RoNBeta.js有点吓人...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM