这怎么评价为假呢?

发布于 2024-12-05 18:29:03 字数 1186 浏览 0 评论 0原文

google chrome 开发者工具的屏幕截图显示这两个对象都有值

当我单步执行此代码时,图中的值会导致 cssLoaded = 0;据我所见,长度大于 0 并且 cssStylesheet.sheet 具有属性,因此应该为 true,因此 cssLoaded 应设置为 1,但它没有发生。我一定在这里遗漏了一些东西..?

function _cssIsLoaded(cssStylesheet, stylePath) {
var cssLoaded = 0;
if (tryCount == 3){
    console.log('oops');
}
if (cssStylesheet.href == stylePath){
    try {
        if ( cssStylesheet.sheet && cssStylesheet.sheet.cssRules.length > 0 ){
            cssLoaded = 1;}
        else if ( cssStylesheet.styleSheet && cssStylesheet.styleSheet.cssText.length > 0 ){
            cssLoaded = 1;}
        else if ( cssStylesheet.innerHTML && cssStylesheet.innerHTML.length > 0 ){
            cssLoaded = 1;}
        }
        catch(ex){ }
}
    if(cssLoaded) {
        //alert('cssloadedcomplete');
        resetPops();
        $('#video-overlay').show();
        positionElements();
        saveBizzmail();
        console.log('try:' + tryCount)
    } else {
        tryCount+=1;
        console.log('try:' + tryCount);
        setTimeout(function() { this._cssIsLoaded(cssStylesheet); }, 2000);
    }
}

screenshot of google chrome developer tools showing that both objects have value

When I step through this code, the values pictured result in cssLoaded = 0; From what I can see, the length is greater than 0 and cssStylesheet.sheet has properties so should be true, therefore cssLoaded should be set to 1, however it's not happening. I must be missing something here..?

function _cssIsLoaded(cssStylesheet, stylePath) {
var cssLoaded = 0;
if (tryCount == 3){
    console.log('oops');
}
if (cssStylesheet.href == stylePath){
    try {
        if ( cssStylesheet.sheet && cssStylesheet.sheet.cssRules.length > 0 ){
            cssLoaded = 1;}
        else if ( cssStylesheet.styleSheet && cssStylesheet.styleSheet.cssText.length > 0 ){
            cssLoaded = 1;}
        else if ( cssStylesheet.innerHTML && cssStylesheet.innerHTML.length > 0 ){
            cssLoaded = 1;}
        }
        catch(ex){ }
}
    if(cssLoaded) {
        //alert('cssloadedcomplete');
        resetPops();
        $('#video-overlay').show();
        positionElements();
        saveBizzmail();
        console.log('try:' + tryCount)
    } else {
        tryCount+=1;
        console.log('try:' + tryCount);
        setTimeout(function() { this._cssIsLoaded(cssStylesheet); }, 2000);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南街女流氓 2024-12-12 18:29:03

您的代码可以通过三种方式获得 cssLoaded == 0;

  1. if (cssStylesheet.href == stylePath) 计算结果为 false
  2. 所有三个 if/else 语句的计算结果均为 false
  3. 其中一个 if/else 语句引发异常,因此其他 if/else 语句不会执行。

在解决问题时,您应该验证前两个不是问题,因为这些可能只是比较中的逻辑错误,或者进入函数的数据不是您所期望的。

对于第三个问题,您可以实现防御性编码,以确保通过将代码更改为此来检查所有三个 if/else 语句(您甚至不再需要 try/catch,因为它允许您的代码绕过一些测试):

function _cssIsLoaded(cssStylesheet, stylePath) {
    var cssLoaded = 0;
    if (tryCount == 3){
        console.log('oops');
    }
    if (cssStylesheet && cssStylesheet.href == stylePath) {
        if ( cssStylesheet.sheet && cssStylesheet.sheet.cssRules && cssStylesheet.sheet.cssRules.length > 0 ) {
            cssLoaded = 1;
        }
        else if ( cssStylesheet.styleSheet && cssStylesheet.styleSheet.cssText && cssStylesheet.styleSheet.cssText.length > 0 ) {
            cssLoaded = 1;
        }
        else if ( cssStylesheet.innerHTML && cssStylesheet.innerHTML.length > 0 ) {
            cssLoaded = 1;
        }
    }
    if(cssLoaded) {
        //alert('cssloadedcomplete');
        resetPops();
        $('#video-overlay').show();
        positionElements();
        saveBizzmail();
        console.log('try:' + tryCount)
    } else {
        tryCount+=1;
        console.log('try:' + tryCount);
        setTimeout(function() { this._cssIsLoaded(cssStylesheet); }, 2000);
    }
}

There are three possible ways your code gets cssLoaded == 0;

  1. if (cssStylesheet.href == stylePath) evaluates to false.
  2. All three if/else statements evaluate to false.
  3. One of the if/else statements throws an exception and thus the other if/else statements aren't executed.

In troubleshooting your issue, you should verify that the first two are not the issue as those are probably just logic errors in the comparisons or the data coming into the function isn't what you expected it to be.

For the third issue, you can implement defensive coding to make sure that all three if/else statements are checked by changing your code to this (you don't even need the try/catch any more because it allows your code to bypass some of the tests):

function _cssIsLoaded(cssStylesheet, stylePath) {
    var cssLoaded = 0;
    if (tryCount == 3){
        console.log('oops');
    }
    if (cssStylesheet && cssStylesheet.href == stylePath) {
        if ( cssStylesheet.sheet && cssStylesheet.sheet.cssRules && cssStylesheet.sheet.cssRules.length > 0 ) {
            cssLoaded = 1;
        }
        else if ( cssStylesheet.styleSheet && cssStylesheet.styleSheet.cssText && cssStylesheet.styleSheet.cssText.length > 0 ) {
            cssLoaded = 1;
        }
        else if ( cssStylesheet.innerHTML && cssStylesheet.innerHTML.length > 0 ) {
            cssLoaded = 1;
        }
    }
    if(cssLoaded) {
        //alert('cssloadedcomplete');
        resetPops();
        $('#video-overlay').show();
        positionElements();
        saveBizzmail();
        console.log('try:' + tryCount)
    } else {
        tryCount+=1;
        console.log('try:' + tryCount);
        setTimeout(function() { this._cssIsLoaded(cssStylesheet); }, 2000);
    }
}
成熟稳重的好男人 2024-12-12 18:29:03

前一个检查可确保您避免类似 NPE 的情况(通过检查 cssStylesheet.sheet 不为 null)。

The former check ensures that you avoid an NPE like situation (by checking that cssStylesheet.sheet is not null).

一绘本一梦想 2024-12-12 18:29:03

您必须检查该元素是否存在,否则可能会发生错误(cssStylesheet.sheet is undefined)。在以下情况下,表达式的计算结果为 false:

  • 变量不存在(undefinednullvoid 0
  • 等于 0 code>(零)
  • 等于 false
  • 等于空字符串 (""'')
  • 非数字值 (0/0,NaN

请注意,空对象 {} 的计算结果为 true,因为它是一个对象。

另一个例子:

if(window.addEventListener) window.addEventListener("load", function(){}, true);
else if(window.attachEvent) window.attachEvent("onload", function(){});
else window.onload = function();

如果不存在 if 条件,则代码将在旧版本的 IE 中抛出错误,因为旧版本的 IE 没有名为 window.addEventListener 的方法。

You have to check for the existence of the element, otherwise an error may occur (cssStylesheet.sheet is undefined). An expression will evaluate to false when:

  • The variable doesn't exist (undefined, null, void 0)
  • Equals 0 (zero)
  • Equals false
  • Equals an empty string ("", '')
  • A Not-a-number value (0/0, NaN)

Note that an empty object {} evaluates to true, because it's an object.

Another example:

if(window.addEventListener) window.addEventListener("load", function(){}, true);
else if(window.attachEvent) window.attachEvent("onload", function(){});
else window.onload = function();

If the if-conditions were not present, the code will throw an error at older versions of IE, which don't have such a method called window.addEventListener.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文