1

I have es6 class with constructor and validation method.

class Popups {
  constructor(selector) {
    this.app = selector;
  }

  validate() {
    $.validator.addMethod('atLeastOneLowercaseLetter', (value, element) => {
        return this.optional(element) || /[a-z]+/.test(value);
        // how to replace `this` ??
      },
      'Must have at least one lowercase letter'
    );
  }
}

My this refers to my Class, but I need get $.validator with my validate form. How i can replace this?

If i write $.validator.optional(element) || /[a-z]+/.test(value) I get error $.validator.optional is not a function

1 Answers1

1

In that case don't use an arrow function as it does not have it's own this. Use a normal function expression as the callback instead.

Kristianmitk
  • 4,528
  • 5
  • 26
  • 46