简体   繁体   English

Dreamweaver CC中的奇怪Javascript验证

[英]Strange Javascript validation in dreamweaver CC

I've installed Dreamweaver CC 2015 and found out that I have MYRIAD errors in my working JavaScript files. 我安装了Dreamweaver CC 2015,发现我在工作的JavaScript文件中有MYRIAD错误。 Also I have MYRIAD errors in imported JavaScript libraries, including jQuery. 我在导入的JavaScript库中也有MYRIAD错误,包括jQuery。

The most important "error" is this one in the beginning of every working function: 最重要的“错误”是每个工作函数开头的这个错误:

Missing "use strict" statement.

It worked quite well without "use strict" and I've never even seen this statement anywhere. 没有“严格使用”,它工作得很好,我甚至从未在任何地方看到这种说法。

Another strange one is: 另一个奇怪的是:

Extending prototype of native object: 'Array'.

Here is the code which provokes the warning: 以下是引发警告的代码:

Array.prototype.sortOn = function(key){
    this.sort(function(a, b){
        if(a[key] < b[key]){
            return -1;
        }else if(a[key] > b[key]){
            return 1;
        }
        return 0;
    });
};

So my options are: 所以我的选择是:

  1. Ditch Dreamweaver and use another IDE (the worst, because it works perfectly fine for my purposes - I'm sole HTML/CSS/JS/PHP/MySQL developer in my project. Ditch Dreamweaver并使用另一个IDE(最糟糕的是,因为它完全适用于我的目的 - 我是我项目中唯一的HTML / CSS / JS / PHP / MySQL开发人员。
  2. Fix all errors like Dreamweaver wants, because it has a good point. 修复Dreamweaver想要的所有错误,因为它有一个好点。 Then please explain why? 那请解释原因? I'm OK with changing "==" to "===", adding "var" before variable declarations, not using "return" after "if" without curly braces, but that "use strict" thing bothers me. 我可以将“==”更改为“===”,在变量声明之前添加“var”,而不是在“if”之后使用“return”而不使用花括号,但“使用严格”的东西困扰我。
  3. Tweak the JavaScript validation, so only critical errors are shown - the best option for me - but I don't know HOW to do it? 调整JavaScript验证,所以只显示严重错误 - 对我来说是最好的选择 - 但我不知道如何做到这一点?

What's the best option to go with? 什么是最好的选择? Any help greatly appreciated. 任何帮助非常感谢。

Here's what I figured out. 这是我想出来的。

The problem was that I was not aware of JSHint and JSLint solutions for validating javascript. 问题是我不知道用于验证javascript的JSHint和JSLint解决方案。 JSHint is integrated into Dreamweaver CC and is easily configurable, provided you know what to do. JSHint已集成到Dreamweaver CC中,并且只要您知道该怎么做,就可以轻松配置。

Normally JSHint has three ways to tweak it , but they don't work in the Dreamweaver environment. 通常,JSHint 有三种方法可以调整它 ,但它们在Dreamweaver环境中不起作用。

What you should do is: 你应该做的是:

  • From the top menu select Edit -> Preferences (Dreamweaver -> Preferences on Mac) 从顶部菜单中选择编辑 - >首选项(Dreamweaver - > Mac上的首选项)
  • Choose "Linting" from the side menu. 从侧边菜单中选择“Linting”。
  • Select "JS" and click "Edit and save changes". 选择“JS”并单击“编辑并保存更改”。
  • The file JS.jshintrc will be opened in Dreamweaver. JS.jshintrc文件将在Dreamweaver中打开。
  • Edit the file like you normally would ( see JSHint options ) and save. 像往常一样编辑文件( 参见JSHint选项 )并保存。

In my specific case I changed this: 在我的具体情况下,我改变了这个:

"strict":false,
"undef":false,
"unused":false

...which means that I don't have to put "use strict" everywhere and may leave definitions and usage of variables in different files. ...这意味着我不必在任何地方放置“使用严格”,并且可能在不同文件中留下变量的定义和用法。 YES, I don't use wrapper functions and use lots of globals. 是的,我不使用包装函数并使用大量的全局变量。 YES, I know it's bad, but refactoring this is not the highest priority in my schedule. 是的,我知道这很糟糕,但重构这不是我日程安排中的最高优先级。

Of course I will change all three those options to "true" and refactor all my JS files to comply to standards when the time comes. 当然,我会将所有这三个选项更改为“true”,并在时机成熟时重构我的所有JS文件以符合标准。

Turning off the "undef" check is a sledge hammer, as JSHint will then ignore all variable and function name typos. 关闭“undef”检查是一个大锤,因为JSHint将忽略所有变量和函数名称拼写错误。 You won't find the typo until run time, which is annoying and time wasting. 你不会在运行时找到拼写错误,这很烦人,浪费时间。

Rather than disabling the "undef" messages completely, I've been using JSHint Inline Directives near the top of my .js files. 我没有完全禁用“undef”消息,而是在我的.js文件顶部附近使用JSHint内联指令 For example: 例如:

/* globals myGlobalVar, myGlobalFunction */
var myLocalVar = myGlobalVar;
myGlobalFunction();

With the globals directive in the file, JSHint will not mark myGlobalVar or myGlobalFunction() as undefined errors. 使用文件中的globals指令, myGlobalVar不会将myGlobalVarmyGlobalFunction()标记为未定义的错误。 Using "globals" inline directives has the added benefit of documenting the dependencies between your .js files. 使用“globals”内联指令还有一个额外的好处,就是记录.js文件之间的依赖关系。

Read about JSHint inline directives here: 在这里阅读JSHint内联指令:

http://jshint.com/docs/#inline-configuration http://jshint.com/docs/#inline-configuration

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

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