简体   繁体   English

JSLint:构造函数中意外悬挂。 为什么?

[英]JSLint: Unexpected dangling in a constructor function. Why?

I have a constructor function: 我有一个构造函数:

function Constructor(parameter) {
    this._property = parameter;
}

and later 然后

Constructor.prototype.someFunction = function() {...}

JSLint is complaining JSLint抱怨

Unexpected dangling '_' in '_property' '_property'中意外悬挂'_'

But when I read the explanation to this warning I find this : 但是,当我阅读此警告的说明时,会发现:

ESLint only raises this warning for variable and function identifiers and not for object property identifiers. ESLint仅针对变量和函数标识符而不是对象属性标识符引发此警告。 jslinterrors.com jslinterrors.com

I thought this._property would be an object property... so I would expect that JSLint should be happy with that. 我以为this._property将是一个对象属性...所以我希望JSLint应该对此感到满意。 What am I doing wrong here? 我在这里做错了什么?

js doesn't have notion of"private" & identifiers prefixed with the underscore character are often used to indicate a private variable but here it does not provide any privacy js没有“ private”的概念,带有下划线字符的标识符通常用于指示私有变量,但此处不提供任何隐私

If you're using JSLint , you can fix the error by setting the nomen (nomenclature) option to true . 如果使用的是JSLint ,则可以通过将nomen (命名法)选项设置为true来修复错误。

/*jslint nomen: true */
function Constructor(parameter) {
    this._property = parameter;
}

You can see for yourself by logging your Constructor that this isn't added to the front of methods/variables: 您可以通过登录自己的构造方法来自己查看, this并未添加到方法/变量的前面:

JSBIN of the code below JSBIN下面的代码

function Constructor(parameter) {
  this._property = parameter;
  this.property = parameter;
}

var x = new Constructor(5);
console.log(x); 
// logs: 
{
  _property: 5, // dangling error
  property: 5
}

You can follow directions to turn off notification of the error HERE 您可以按照指示在此处关闭错误通知

or not use an underscore. 或不使用下划线。

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

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