在Javascript中,为什么验证器请求'new'在函数调用之前
In Javascript, why is validator requesting 'new' in front of a function invocation?
我使用的是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中可能会令人困惑——即使有命名约定——因为您可以创建一个不真正构造东西的构造函数,调用没有父函数的构造函数,以及许多其他有趣的语言特性。
相关文章:
- 是否可以将一个函数输入连接到另一个函数调用的文本
- 在输入字段上有两个函数调用,一个在Blur上,一个不在Angular中
- 如何在Javascript函数调用中循环变量
- Javascript:应为赋值或函数调用,但实际看到的却是表达式
- 如何远程检查JavaScript应用程序的函数调用堆栈
- javascript函数调用不起作用
- 为什么这个函数调用会破坏程序并导致未定义的变量
- 如何通过函数调用设置图像的src
- 从全局函数调用Ember控制器上的方法
- 为什么Jquery$.ajax在函数调用中触发所有statusCode,即使调用成功
- JavaScript函数调用(arg1)(arg2)
- 打印链接时,将javascript函数调用到链接中
- 在函数调用中封装数据除了隐藏数据之外还有什么优点
- 无法从JavaScript中的函数调用对象属性
- 在 new Model() 上返回一个函数,然后能够在返回的函数上再次调用 new
- 当调用返回相同对象的函数时,无论是否作为构造函数调用,我都应该使用“new”吗
- “new"匿名函数调用前返回对象
- 我们可以在箭头函数上调用new吗?
- 在Javascript中,为什么验证器请求'new'在函数调用之前
- 是所有的函数声明&在幕后调用new Function()创建的表达式