Call for an internal javascript function



  • How to trigger an internal javascript the function

    function validate(params, callback) {
         // some code....
         this.results = function () {
               alert('work!');
          } 
    
      callback();
    

    }

    var res = function () {
    validate.results(); // здесь появляется ошибка
    }

    validate (params, res);

    Trying to cause an internal function through the calibre but getting a mistake. validate.results is not a function



  • One of the solutions is simply transferring the function. results in callback

    function validate(params, callback) {
         // some code....
         function results() {
               alert('work!');
          } 
    
      callback(results);
    

    }

    var res = function (validateResults) {
    validateResults(); // все ОК
    }

    validate (params, res);

    One more instead. this use the name of the function, the meaning this Depends on how the function was caused.

    function validate(params, callback) {
    // some code....
    validate.results = function () {
    alert('work!');
    }

      callback();
    

    }

    var res = function () {
    validate.results(); // здесь появляется ошибка
    }
    validate (params, res);

    Another solution is to use the operator new

    function validate(params, callback) {
    // some code....
    this.results = function () {
    alert('work!');
    }

      callback(this);
    

    }

    var res = function (validate) {
    validate.results(); // здесь появляется ошибка
    }
    new validate (params, res);

    Another solution is to use the operator new, version 2

    function validate(params, callback) {
    // some code....
    this.results = function () {
    alert('work!');
    }

      callback();
    

    }

    var res = function () {
    validateObj.results(); // здесь появляется ошибка
    }
    var validateObj = new validate (params, res);




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2