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