简体   繁体   English

访问Javascript变量

[英]Accessing Javascript variable

In the following code I am accessing the cookieCode in the var MasterTmsUdo but I am unable to access this variable. 在下面的代码中,我访问var MasterTmsUdo中的var MasterTmsUdo但我无法访问此变量。 Can anyone tell me how can I access this variable inside var MasterTmsUdo ? 谁能告诉我如何在var MasterTmsUdo访问这个变量?

var cookieCode="True";
console.log(cookieCode);
var MasterTmsUdo = { 'CJ' : { 'CID': ' 897415', 'TYPE': '894', 'AMOUNT' : '35.00', 'OID' : "115", 'CURRENCY' : 'USD','FIRECJ' : cookieCode, } }; 

It's a property of the CJ object, therefore you can access it via: 它是CJ对象的属性,因此您可以通过以下方式访问它:

MasterTmsUdo.CJ.FIRECJ

Therefore you could have: 因此你可以:

var cookieCode="True";
console.log(cookieCode);
var MasterTmsUdo = { 'CJ' : { 'CID': ' 897415', 'TYPE': '894', 'AMOUNT' : '35.00', 'OID' : "115", 'CURRENCY' : 'USD','FIRECJ' : cookieCode } };
console.log(MasterTmsUdo.CJ.FIRECJ);

Access the variable by using the following code: 使用以下代码访问变量:

var FireCJ = MasterTmsUdo['CJ']['FIRECJ'];

OR 要么

var FireCJ = MasterTmsUdo.CJ.FIRECJ;

You can then alert or console.log the code and it will return the string "True". 然后,您可以提醒或console.log代码,它将返回字符串 “True”。 FireCJ can also be used in other code to make some IF statements for example 例如,FireCJ也可以在其他代码中用于制作一些IF语句
Small note: You can either use a boolean type (true or false) instead of a string type containing 'True'. 小注意:您可以使用布尔类型(true或false)而不是包含'True'的字符串类型。

IF statement with boolean: 带有布尔值的IF语句:

if(FireCJ) {
    // TRUE
} 

IF statement with your code: IF语句与您的代码:

if(FireCJ == 'True') {
    // TRUE
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM