0

当我尝试检查 [action] 是否已定义时,我不断收到 javascript 错误。

    if((typeof array_from_php.api_description[mobile_type][action]) != 'undefined') {
        console.log('defined');
        api = array_from_php.api_description[mobile_type][action];
    } else {
        console.log('undefined');
        mobile_type = 0;
        api = array_from_php.api_description[mobile_type][action];
    }

错误:未捕获的类型错误:无法读取未定义的属性“register_mobile”

4

1 回答 1

1

你需要检查每个属性

if(array_from_php && array_from_php.api_description && array_from_php.api_description[mobile_type] && (typeof array_from_php.api_description[mobile_type][action]) != 'undefined') {
    console.log('defined');
    api = array_from_php.api_description[mobile_type][action];
} else {
    console.log('undefined');
    mobile_type = 0;
    api = array_from_php.api_description[mobile_type][action];
}
于 2013-08-15T12:59:38.057 回答