javascript – 错误:调用构造函数时缺少新的前缀

前端之家收集整理的这篇文章主要介绍了javascript – 错误:调用构造函数时缺少新的前缀前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在node.js中创建一个函数.以下是相关代码,当我调用函数时它给我错误.
function ReplacePlaceholders() {
            return 'success';
          }      



  exports.sendMailMsg = function (templateName,receiverEmail,dataPlaceholders) {

        ReplacePlaceholders();
}

解决方法

在node.js中,函数名称为camel cased,应以小写字符开头.使用大写字符开始一个函数,告诉JSHint将函数考虑在一个构造函数而不是一个方法上.

这实际上是由JSHint生成错误,但代码将正确运行. The option in JSHint,newcap,导致此错误实际上是折旧,并禁用它是建议的.

关于为什么这个选项甚至在JSHint中的相关信息:

This option requires you to capitalize names of constructor functions. Capitalizing functions that are intended to be used with new operator is just a convention that helps programmers to visually distinguish constructor functions from other types of functions to help spot mistakes when using this.

Not doing so won’t break your code in any browsers or environments but it will be a bit harder to figure out—by reading the code—if the function was supposed to be used with or without new. And this is important because when the function that was intended to be used with new is used without it,this will point to the global object instead of a new object.

猜你在找的JavaScript相关文章