带有 ECMAScript6 的 JSHint:未定义方法
JSHint with ECMAScript6: method is not defined
我正在使用 ECMAScript6 实现客户端应用程序并使用 JSHint 进行静态代码分析。我经常在我的代码中使用以下模式:
class MyClass {
constructor() {
//This is how I would like to call myMethod
myMethod();
//This is how I should call myMethod to make JSHint analysis pass
this.myMethod();
}
myMethod(){
//Implementation
}
}
我的主要语言是 Java,所以我希望只需调用 myMethod() 就可以了。但是,如果不将 this 添加到方法调用中,我会收到来自 JSHint 的“'myMethod' 未定义”警告。我的问题是:
- 在这种情况下,没有this拨打电话是否正确? (例如,在 PHP 中你总是需要添加 $this-> 到非静态方法调用)
- 如果没有这个 调用是正确的 有没有办法(任何 .jshintrc 标志)在 JSHint 中关闭这个警告?
不,这在 JavaScript 中是正确的,而且从来都不正确。方法总是需要在接收器上显式调用 make this
work, and they need to be referred to using property access notation because methods are just functions on properties in javascript. They're not available as functions in the scope of your other methods. It's the same for properties,顺便说一句。
JsHint 就在这里,没有理由关闭该警告。即使这可能,执行你的程序只会让它无法工作。
在您提供的代码中,标识符 myMethod
未定义,但 MyClass
实例的继承 属性 myMethod 是定义。
如果您将 myMethod
定义为闭包下的 函数 ,而其他地方不可用,那么您可以按照您想要的形式访问它
var MyClass = (function () {
function myMethod() {
//Implementation
}
class MyClass {
constructor() {
myMethod();
}
}
return MyClass;
}());
我不会写太多 ES6,所以我不确定将 function myMethod
放在 MyClass
的定义中是否是一个 SyntaxError
但是请注意,需要 this
来引用您的 MyClass
的特定实例,因此如果您希望 MyMethod
对实例.
function myMethod(obj) {...}
// ...
myMethod(this);
如果您阅读 MDN's description of class
JavaScript classes are introduced in ECMAScript 6 and are syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax is not introducing a new object-oriented inheritance model to JavaScript. JS classes provide a much simpler and clearer syntax to create objects and dealing with inheritance.
这就是说使用 class
只是 shorthand 的旧方法,而不是新模型,因此可能更容易想到您当前的代码如果用 ES5,
编写
var MyClass = (function () {
function MyClass() {
this.constructor.apply(this, arguments);
}
MyClass.prototype = Object.create(null);
MyClass.prototype.constructor = function () {
myMethod(); // referenceError
this.myMethod(); // works
};
MyClass.prototype.myMethod = function () {
//Implementation
};
return MyClass;
}());
Is it correct to make calls without this in such situation? (e.g. in
PHP you always need to add $this-> to non-static method call)
不,不是。您始终必须指定方法的接收者。
If that's correct to make calls without this is there any way (any
.jshintrc flag) to turn off this warning in JSHint?
JSHint returns “'myMethod' 未定义”警告正确,因为在构造函数的范围内没有调用 myMethod
的函数。
我正在使用 ECMAScript6 实现客户端应用程序并使用 JSHint 进行静态代码分析。我经常在我的代码中使用以下模式:
class MyClass {
constructor() {
//This is how I would like to call myMethod
myMethod();
//This is how I should call myMethod to make JSHint analysis pass
this.myMethod();
}
myMethod(){
//Implementation
}
}
我的主要语言是 Java,所以我希望只需调用 myMethod() 就可以了。但是,如果不将 this 添加到方法调用中,我会收到来自 JSHint 的“'myMethod' 未定义”警告。我的问题是:
- 在这种情况下,没有this拨打电话是否正确? (例如,在 PHP 中你总是需要添加 $this-> 到非静态方法调用)
- 如果没有这个 调用是正确的 有没有办法(任何 .jshintrc 标志)在 JSHint 中关闭这个警告?
不,这在 JavaScript 中是正确的,而且从来都不正确。方法总是需要在接收器上显式调用 make this
work, and they need to be referred to using property access notation because methods are just functions on properties in javascript. They're not available as functions in the scope of your other methods. It's the same for properties,顺便说一句。
JsHint 就在这里,没有理由关闭该警告。即使这可能,执行你的程序只会让它无法工作。
在您提供的代码中,标识符 myMethod
未定义,但 MyClass
实例的继承 属性 myMethod 是定义。
如果您将 myMethod
定义为闭包下的 函数 ,而其他地方不可用,那么您可以按照您想要的形式访问它
var MyClass = (function () {
function myMethod() {
//Implementation
}
class MyClass {
constructor() {
myMethod();
}
}
return MyClass;
}());
我不会写太多 ES6,所以我不确定将 function myMethod
放在 MyClass
的定义中是否是一个 SyntaxError
但是请注意,需要 this
来引用您的 MyClass
的特定实例,因此如果您希望 MyMethod
对实例.
function myMethod(obj) {...}
// ...
myMethod(this);
如果您阅读 MDN's description of class
JavaScript classes are introduced in ECMAScript 6 and are syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax is not introducing a new object-oriented inheritance model to JavaScript. JS classes provide a much simpler and clearer syntax to create objects and dealing with inheritance.
这就是说使用 class
只是 shorthand 的旧方法,而不是新模型,因此可能更容易想到您当前的代码如果用 ES5,
var MyClass = (function () {
function MyClass() {
this.constructor.apply(this, arguments);
}
MyClass.prototype = Object.create(null);
MyClass.prototype.constructor = function () {
myMethod(); // referenceError
this.myMethod(); // works
};
MyClass.prototype.myMethod = function () {
//Implementation
};
return MyClass;
}());
Is it correct to make calls without this in such situation? (e.g. in PHP you always need to add $this-> to non-static method call)
不,不是。您始终必须指定方法的接收者。
If that's correct to make calls without this is there any way (any .jshintrc flag) to turn off this warning in JSHint?
JSHint returns “'myMethod' 未定义”警告正确,因为在构造函数的范围内没有调用 myMethod
的函数。