2017-08-29 5 views
0

このロケール変数をメインスクリプトに取得する方法。どのようにしてグローバル変数を作成できますか?この変形はうまくいかず、助けてくれてありがとう。このロケール変数をメインスクリプトに取得する方法。

//Test Script local to global 
 

 
    (function (exampleCode) { 
 
\t  "use strict"; 
 
\t 
 
    var wert = 0.5; 
 
    var name = 'wert'; 
 

 
    Object.assign(exampleCode, { 
 
    
 
    \t  getValue: function() { 
 
    
 
    \t  return name; 
 
     
 
\t  } 
 
    
 
\t  }); 
 
\t 
 
    })(window.exampleCode = window.exampleCode || {}); 
 

 

 
    ///////////////////////////////////////////////////////////////////// 
 

 
    //main script get 'var wert = 0.5' from Test Script 
 

 
     var update = function() { 
 
\t \t  requestAnimationFrame(update); \t \t \t \t  \t \t 
 
\t \t \t  //var value = wert;  //0.5 
 
\t \t \t  console.log(exampleCode.getValue()); 
 
\t \t \t  mesh.morphTargetInfluences[ 40 ] = params.influence1 = value; \t \t 
 
\t \t }; \t 
 
\t \t \t 
 
\t \t update(); \t \t

答えて

0

私が正しくあなたを理解している場合は、その変数を取得するためにgetValue機能の外側のスコープへのリンクが必要です。したがって、あなたのデータをオブジェクトに格納してからmyVars[propName];getValue(propName)に返すと、取得したいプロパティの名前を渡すことができます。後でその値(0.5)をクロージャーにかけます。

(function (exampleCode) { 
 
    "use strict"; 
 
\t 
 
    let myVars = { 
 
     wert: 0.5 
 
    }; 
 

 
    Object.assign(exampleCode, { 
 
     getValue: propName => myVars[propName] 
 
    }); 
 
\t 
 
    })(window.exampleCode = window.exampleCode || {}); 
 
    
 
    let update = function() { \t \t \t  \t \t 
 
     let value = exampleCode.getValue('wert');  //0.5 
 
\t  console.log(value); 
 
\t  //mesh.morphTargetInfluences[ 40 ] = params.influence1 = value; \t \t 
 
\t }; \t 
 
\t \t \t 
 
\t update(); \t

代替はexampleCodewert財産を作り、thisを通じてその値を取ることです。

(function (exampleCode) { 
 
    "use strict"; 
 

 
    Object.assign(exampleCode, { 
 
     getValue: function(propName) { return this[propName]; }, 
 
     wert: 0.5 
 
    }); 
 
    \t 
 
})(window.exampleCode = window.exampleCode || {}); 
 
     
 
let update = function() { \t \t \t  \t \t 
 
    let value = exampleCode.getValue('wert'); 
 
    console.log(value); \t \t 
 
}; \t 
 
    \t \t \t 
 
update(); \t

+0

はい、まさにそれ以降、私は検索しました、感謝します君は – Tom

関連する問題