0

I'm implementing a client-side application using ECMAScript6 and use JSHint for static code analysis. I often use the following pattern in my code:

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
    }
}

My primary language is Java so I expect that simply calling myMethod() should be ok. However without adding this to method call I'm getting "'myMethod' is not defined" warning from JSHint. My questions are:

  1. 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)
  2. If that's correct to make calls without this is there any way (any .jshintrc flag) to turn off this warning in JSHint?
vania-pooh
  • 2,588
  • 3
  • 20
  • 39

3 Answers3

5

No, this is and never was correct in JavaScript. Methods always need to be called on a receiver explicitly to 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, btw.

JsHint is right here, and there's no reason to turn that warning off. Even if that may possible, executing your program in spite of that would just make it not work.

Community
  • 1
  • 1
Bergi
  • 513,640
  • 108
  • 821
  • 1,164
1

In the code you provided the identifier myMethod isn't defined, but the inherited property myMethod of instances of MyClass is defined.

If you define myMethod as a Function under a closure which isn't available elsewhere then you can access as it in the form you desire

var MyClass = (function () {
    function myMethod() {
        //Implementation
    }

    class MyClass {
        constructor() {
            myMethod();
        }
    }
    return MyClass;
}());

I don't get to write much ES6 so I'm not sure if putting the function myMethod inside MyClass's definition is a SyntaxError

Please note however that this is required to reference your specific instance of MyClass, so you'll probably need to use it somewhere if you want MyMethod to act on the instance.

function myMethod(obj) {...}
// ...
        myMethod(this);

If you read the 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.

This is saying using class is just shorthand for the old way of doing it, not a new model, so it may be easier to think of what your current code would look like if written in 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;
}());
Paul S.
  • 58,277
  • 8
  • 106
  • 120
1

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)

No, it is not. You always have to specify the receiver of the method.

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' is not defined" warning correctly as there is not function called myMethod in the scope of the constructor.

Giuseppe Pes
  • 7,070
  • 3
  • 41
  • 80