Javascript call nested function

2019-01-05 03:36发布

I have the following piece of code:

function initValidation()
{
    // irrelevant code here
    function validate(_block){
        // code here
    }
}

Is there any way I can call the validate() function outside the initValidation() function? I've tried calling validate() but I think it's only visible inside the parent function.

6条回答
对你真心纯属浪费
2楼-- · 2019-01-05 04:06

Hope that you are looking for something like this

function initValidation()
{
    // irrelevant code here
    this.validate = function(_block){
        // code here
    }
}

var fCall = new initValidation()
fCall.validate(param);

This will work.

Hope this addresses your problem.

查看更多
Fickle 薄情
3楼-- · 2019-01-05 04:07

This invocation will return function statement, which is function validate. So you can invoke directly after the first invocation.

function initValidation() {
  // irrelevant code here
  return function validate(_block) {
    // code here
  }
}

initValidation()();
查看更多
疯言疯语
4楼-- · 2019-01-05 04:09

I know this is an old post but if you wish to create a set of instances that you wish to work with that reuse the code you could do something like this:

"use strict";
// this is derived from several posts here on SO and ultimately John Resig
function makeClassStrict() {
  var isInternal, instance;
  var constructor = function(args) {
    if (this instanceof constructor) {
      if (typeof this.init == "function") {
        this.init.apply(this, isInternal ? args : arguments);
      }
    } else {
      isInternal = true;
      instance = new constructor(arguments);
      isInternal = false;
      return instance;
    }
  };
  return constructor;
}
var MyClass = makeClassStrict();// create "class"
MyClass.prototype.init = function(employeeName, isWorking) {
  var defaultName = 'notbob';
  this.name = employeeName ? employeeName : defaultName;
  this.working = !!isWorking;
  this.internalValidate = function() {
    return {
      "check": this.working,
      "who": this.name
    };
  };
};
MyClass.prototype.getName = function() {
  return this.name
};
MyClass.prototype.protoValidate = function() {
return {
      "check": this.working,
      "who": this.name
    };
};
var instanceBob = MyClass("Bob", true);// create instance
var instanceFred = MyClass("Fred", false);// create instance
var mything = instanceFred.internalValidate();// call instance function
console.log(mything.check + ":" + mything.who);
var myBobthing = instanceBob.protoValidate();
console.log(myBobthing.check + ":" + myBobthing.who);
查看更多
霸刀☆藐视天下
5楼-- · 2019-01-05 04:13

You can call validate from within initValidation. Like this.

function initValidation()
{
    // irrelevant code here
    function validate(_block){
        // code here
    }

    return validate(someVar);
}

validate is not visible to anything outside of initValidation because of its scope.

Edit: Here's my suggestion of a solution.

(function() {
    function validate(_block){
        // code here
    }

    function initValidation()
    {
        // irrelevant code here

        return validate(someVar);
    }

    function otherFunctions() {
        // ...
    }

    // initValidation = function
}());

// initValidation = undefined

All of your functions will be hidden to anything outside the function wrapper but can all see each other.

查看更多
Root(大扎)
6楼-- · 2019-01-05 04:18

Should work.

function initValudation() {
    validate();
    function validate() {

    }
}
查看更多
贼婆χ
7楼-- · 2019-01-05 04:21

    function initValidation()
    {
        // irrelevant code here
        function validate(_block){
            console.log( "test", _block );
        }
    
        initValidation.validate = validate;
    }

    initValidation();
    initValidation.validate( "hello" );
    //test hello

查看更多
登录 后发表回答