最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error : missing new prefix when invoking a constructor - Stack Overflow

programmeradmin5浏览0评论

I am trying to create a function in node.js. The following is relevant code, it gives me error when i call function.

function ReplacePlaceholders() {
            return 'success';
          }      



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

        ReplacePlaceholders();
}

I am trying to create a function in node.js. The following is relevant code, it gives me error when i call function.

function ReplacePlaceholders() {
            return 'success';
          }      



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

        ReplacePlaceholders();
}
Share Improve this question edited Feb 6, 2014 at 6:11 Anup asked Feb 6, 2014 at 6:01 AnupAnup 9,73817 gold badges77 silver badges145 bronze badges 1
  • There is nothing wrong with the code you show here unless ReplacePlaceholders() is not in the same scope as exports.sendMailMsg or unless exports is not an object. What specific error do you get? – jfriend00 Commented Feb 6, 2014 at 6:15
Add a comment  | 

3 Answers 3

Reset to default 17

In node.js, function names are camel cased, and should start with a lowercase character. Starting a function with an uppercase character tells JSHint to consider the function a constructor rather than a method.

This is actually an error being generated by JSHint, but the code will run correctly. The option in JSHint, newcap, which causes this error is actually depreciated, and disabling it is recommended.

The relevant info as to why this option is even in 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.

The error message you mention is a JSHint error message, not a runtime error. There is a discussion of it here:

jshint expects the new 'prefix' for functions

JSHint expects functions that start with a capital letter to be object definitions. You can ignore the error, disable it in JSHint, or rename your function so that it starts with a lower-case letter.

I'm not sure why that might be, but that error suggests that doing new ReplacePlaceholders(); might work. Though, you may want to consider something like the following:

function ReplacePlaceholders(templateName, receiverEmail, dataPlaceholders) {
  return 'success';
}

exports.sendMailMsg = ReplacePlaceholders;
发布评论

评论列表(0)

  1. 暂无评论