未捕获的TypeError:无法读取未定义的属性'width_pert'

时间:2017-11-07 07:37:13

标签: javascript node.js

我在线<{p}}收到错误Uncaught TypeError: Cannot read property 'width_pert' of undefined"

var getWidth = (player_value['width_pert']*window.innerWidth)/100;

在以下代码中。

function zoomIn() {
    player_value['width_pert'] = player_value['width_pert'] + 20;
    if(player_value['zindex'] == '1'){              
        //console.log(player_value['width_pert'] + ' ssss');
        $('.viewPresentation').css({
            'width' : player_value['width_pert'] + '%',
            'height' : player_value['width_pert'] + '%'
        });
    } else {
        $('.viewPDF').css({ 
            'width' : player_value['width_pert'] + '%',
            'padding-top' : parseInt(player_value['width_pert']*parseFloat(player_value['ratio'])) + '%'
        });             
        player_value['padding_top_pert'] = parseInt(player_value['width_pert']*parseFloat(player_value['ratio']));
        getPageDim();
    }
}

function zoomOut() {
    //console.log(player_value['width_pert'] + ' ssss 777');
    if(player_value['width_pert'] > 40) {
        player_value['width_pert'] = player_value['width_pert'] - 20;
        if(player_value['zindex'] == '1'){              
            $('.viewPresentation').css({
                'width' : player_value['width_pert'] + '%',
                'height' : player_value['width_pert'] + '%'
            });
        } else {
            $('.viewPDF').css({
                'width' : player_value['width_pert'] + '%',
                'padding-top' : parseInt(player_value['width_pert']*parseFloat(player_value['ratio'])) + '%'
            });
            player_value['padding_top_pert'] = parseInt(player_value['width_pert']*parseFloat(player_value['ratio']));
            getPageDim();
        }
    }
}

function fitwidth(scale) {
    if(player_value['zindex'] == '0'){
        playerState = 'normal';
        player_value['width_pert'] = scale;
        $('.viewPDF').css({
            'width' : player_value['width_pert'] + '%',
            'padding-top' : parseInt(player_value['width_pert']*parseFloat(player_value['ratio'])) + '%'
        });
        player_value['padding_top_pert'] = parseInt(player_value['width_pert']*parseFloat(player_value['ratio']));
        getPageDim();
        clearTimeout(tmrSlideActions);
        tmrSlideActions = setTimeout(positionSlide, 500);
    }
}

function fitpage() {    
    if(player_value['zindex'] == '0'){  
        playerState = 'fitpage';    
        var heightFit = window.innerHeight;
        if(player_value['fixed'] == '1'){
            heightFit = heightFit - parseInt($('#nav').css('height') + 5) ;
        }
        var widthTemp = parseInt(heightFit/player_value['ratio']);
        if(widthTemp > window.innerWidth){
            player_value['width_pert'] = 100;
        } else {
            player_value['width_pert'] = (widthTemp/window.innerWidth)*100; 
        }           
        player_value['padding_top_pert'] = (heightFit / window.innerWidth)*100;
        $('.viewPDF').css({
            'width' : player_value['width_pert'] + '%',
            'padding-top' : player_value['padding_top_pert'] + '%'
        }); 
function getPageDim(){      
    var getWidth = (player_value['width_pert']*window.innerWidth)/100;
    pageHeight = parseFloat(player_value['padding_top_pert']*window.innerWidth/100) + 0.5;                                  
}

不知道为什么会出现此错误。代码中会显示使用width_pert的位置。 width_pert用于获取页面宽度。还有一件事要提到脚本文件中所做的更改没有反映在localhost

1 个答案:

答案 0 :(得分:0)

player_value中没有function getPageDim() {}。也许你可以将它作为参数传递或在较高的范围内声明它。

即使声明为全局,也需要初始化player_value,例如:

var player_value = {};

function zoomIn() { ... }
function zoomOut() { ... }
...

只要您尝试访问尚未实例化的Object字段,就会发生此错误,就像执行undefined['width_pert']一样。

在调用player_value之前,如果getPageDim()在某个地方正确地被隐藏了,请检查您的代码。

一般情况下,您应该始终检查参数是否一致:

function anything(something) {
    if (something !== undefined) { // or just if (something)
        // do my stuff
    }
}