2017-08-29 43 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