Javascript Javascript调用嵌套函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8817872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript call nested function
提问by Eduard Luca
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.
有什么办法可以在validate()
函数外调用initValidation()
函数吗?我试过调用,validate()
但我认为它只在父函数内可见。
回答by Esailija
function initValidation()
{
// irrelevant code here
function validate(_block){
console.log( "test", _block );
}
initValidation.validate = validate;
}
initValidation();
initValidation.validate( "hello" );
//test hello
回答by AmGates
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.
希望这可以解决您的问题。
回答by Olical
You can call validate
from within initValidation
. Like this.
您可以validate
从内部调用initValidation
。像这样。
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.
validate
initValidation
由于其范围,对任何外部都不可见。
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.
您的所有函数都将隐藏在函数包装器之外的任何内容中,但都可以相互看到。
回答by Jonathan Wang
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()();
回答by Mark Schultheiss
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);
回答by Marius Mucenicu
I know this thread's been here for quite some time but I thought I'd also leave my 0.02$ on how to call inner functions from outside their scope (might benefit somebody).
我知道这个线程在这里已经有一段时间了,但我想我也会留下我的 0.02$ 关于如何从它们的范围之外调用内部函数(可能会让某人受益)。
Note that in any place, a better design decision should be taken into consideration rather than some hackish workaround which will bite you back later.
请注意,在任何地方,都应该考虑更好的设计决策,而不是一些会在以后咬你的黑客解决方法。
How about using function expressionsinstead of function statementsand making use of the global scope.
var innerFn;
function outerFn() {
innerFn = function(number) {
return number ** 2;
}
}
outerFn();
console.log(innerFn(5));
// if there's more complex code around and you could write this defensively
if (typeof innerFn !== 'undefined') {
console.log(`we are squaring the number 5 and the result is: ${innerFn(5)}`);
} else {
console.log('function is undefined');
}
Or, you can make use of closures:
或者,您可以使用闭包:
function outer() {
// initialize some parameters, do a bunch of stuff
let x = 5, y = 10;
function inner() {
// keeps references alive to all arguments and parameters in all scopes it references
return `The arithmetic mean of the 2 numbers is: ${(x + y) / 2}`;
}
return inner;
}
innerFn = outer(); // get a reference to the inner function which you can call from outside
console.log(innerFn());
回答by S J
Function definition:
函数定义:
function initValidation() {
// irrelevant code here
function validate(_block){
// code here
}
return validate;
}
Call it as below:
如下调用:
initValidate()(block_value);
回答by Carl Bergquist
Should work.
应该管用。
function initValudation() {
validate();
function validate() {
}
}