在Javascript中,为什么验证器请求'new'在函数调用之前

In Javascript, why is validator requesting 'new' in front of a function invocation?

本文关键字:函数调用 new 请求 Javascript 为什么 验证      更新时间:2023-09-26

我使用的是ECMAScript Edition 6,不确定这是否相关。无论如何,我是新的JavaScript,我试图使一个脚本,检查如果一个文件(.pdf)存在,并打开它,如果它这样做,但如果不是它打开一个默认页面(.html),显示在哪里获得所需的文件(.pdf)。我想避免库,如jQuery, Bootstrap, AJAX…等。我使用"http://www.jslint.com"进行验证。错误是"期望'new'在'UrlExists'之前。"

谢谢!

// These are declarations of global variables for use with   http://www.jslint.com/
/*global window */
/*global document */
/*global alert */
/*global XMLHttpRequest */
"use strict"; // This is not necessary, but helps catch errors if they occur.
//get id
function  $(id)
{
return document.getElementById(id);
}
// This function is called from the click events triggered by the onload or load function.
// It calls the appropriate function depending on what the UrlExists function returns.
function mylinkclicked(mylocalfile, myurl)
{
    if (UrlExists(mylocalfile))
   {
    alert ("It exists!");
   }
      else
      {
      alert ("It does NOT exists!");
      }
}
// This funtion will run when the page fully loads, and without causing any errors.
function after_all_loads_gogogo()
{
$("undergraduatecatalog").onclick = function() {var mylocalfile = "./local_files/catalog.pdf"; var myurl = "./resource_help/catalogdefault.html"; mylinkclicked(mylocalfile, myurl);};
$("classschedule").onclick = function() {var mylocalfile = "./local_files/schedule/schedule.html"; var myurl = "./resource_help/scheduledefault.html"; mylinkclicked(mylocalfile, myurl);};
$("degreerequirements").onclick = function() {var mylocalfile = "./local_files/degreerequirements.pdf"; var myurl = "./resource_help/degreereqdefault.html"; mylinkclicked(mylocalfile, myurl);};
}
if (window.attachEvent) {window.attachEvent('onload', after_all_loads_gogogo);}
else if (window.addEventListener) {window.addEventListener('load', after_all_loads_gogogo, false);}
else {document.addEventListener('load', after_all_loads_gogogo, false);}

对于类和构造函数使用标题大小写,对于一般函数使用驼峰或蛇形大小写是惯例。这个概念,就我所知,是正确地处理对象类型,并区分松散函数和构造函数。

许多JS工具都有一个强制的规则,通常是默认启用的(ESLint的等效是new-cap)。如果您愿意,可以禁用它们,或者遵循它们的编码标准并使用小写字母启动松散的函数。

对类使用titleccase,对函数使用camelCase,可以帮助一目了然地识别类(或静态方法)引用。这在JS中可能会令人困惑——即使有命名约定——因为您可以创建一个不真正构造东西的构造函数,调用没有父函数的构造函数,以及许多其他有趣的语言特性。