私は次のコードを持っています:
function initValidation()
{
// irrelevant code here
function validate(_block){
// code here
}
}
validate()
関数の外部で関数を呼び出す方法はありinitValidation()
ますか?呼び出してみましvalidate()
たが、親関数内にしか表示されないと思います。
私は次のコードを持っています:
function initValidation()
{
// irrelevant code here
function validate(_block){
// code here
}
}
validate()
関数の外部で関数を呼び出す方法はありinitValidation()
ますか?呼び出してみましvalidate()
たが、親関数内にしか表示されないと思います。
function initValidation()
{
// irrelevant code here
function validate(_block){
console.log( "test", _block );
}
initValidation.validate = validate;
}
initValidation();
initValidation.validate( "hello" );
//test hello
あなたがこのようなものを探していることを願っています
function initValidation()
{
// irrelevant code here
this.validate = function(_block){
// code here
}
}
var fCall = new initValidation()
fCall.validate(param);
これは機能します。
これがあなたの問題に対処することを願っています。
validate
内から呼び出すことができますinitValidation
。このような。
function initValidation()
{
// irrelevant code here
function validate(_block){
// code here
}
return validate(someVar);
}
validate
スコープinitValidation
があるため、外部には表示されません。
編集:これが解決策の私の提案です。
(function() {
function validate(_block){
// code here
}
function initValidation()
{
// irrelevant code here
return validate(someVar);
}
function otherFunctions() {
// ...
}
// initValidation = function
}());
// initValidation = undefined
すべての関数は関数ラッパーの外側に隠されますが、お互いを見ることができます。
この呼び出しは、関数検証である関数ステートメントを返します。したがって、最初の呼び出しの直後に呼び出すことができます。
function initValidation() {
// irrelevant code here
return function validate(_block) {
// code here
}
}
initValidation()();
これは古い投稿であることは知っていますが、コードを再利用して操作したいインスタンスのセットを作成したい場合は、次のようにすることができます。
"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);
私はこのスレッドがかなり長い間ここにあることを知っていますが、スコープ外から内部関数を呼び出す方法にも0.02ドルを残したいと思いました(誰かに利益をもたらすかもしれません)。
どの場所でも、後であなたを噛み砕くようなハックの回避策ではなく、より良い設計上の決定を考慮に入れる必要があることに注意してください。
関数ステートメントの代わりに関数式を使用し、グローバルスコープを使用するのはどうですか。
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');
}
または、クロージャを利用できます:
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());
親関数の外部で変数を作成してから、親関数で必要な関数を変数に格納します。
Var Store;
Function blah() {
Function needed() {
#
}
Store = needed;
}
Esailijaの答えのマイナーなバリエーションとして、私はこれを行いました:
function createTree(somearg) {
function validate(_block) {
console.log( "test", _block );
}
if (somearg==="validate") { return validate; } // for addNodes
// normal invocation code here
validate(somearg);
}
function addNodes() {
const validate = createTree("validate");
//...
validate( "hello" );
}
createTree("create");
addNodes();
//validate("illegal");
そのため、validate()はcreateTree()とaddNodes()の間で完全に共有され、外部からは完全に見えなくなりました。
動作するはずです。
function initValudation() {
validate();
function validate() {
}
}
関数の定義:
function initValidation() {
// code here
function validate(_block){
// code here
console.log(_block);
}
return validate;
}
以下のように呼んでください:
initValidation()("hello");
function initValidation()
{
function validate(_block){
console.log(_block)
// code here
}
// you have to call nested function
validate("Its Work")
}
// call initValidation function
initValidation()