【问题标题】:Why can't I access a property of an object in javascript?为什么我不能在 javascript 中访问对象的属性?
【发布时间】:2012-05-28 13:02:58
【问题描述】:

我正在用 javascript 构建自己的照片库,部分是为了体验,部分是为了一个项目。现在,我遇到了一些关于对象的问题。

当一个 Image 对象被创建时,它被推送到 Gallery 对象中的一个数组中。围绕评论// constrain to width(要点中的第55 行),我试图从对象中获取widthheight 属性。如果我 console.log 变量img,我可以看到对象的属性和功能。但是,如果我console.log(img.height);,我会不确定。我做错了什么?

整个文件如下。也是here in a gist,方便阅读。

var Image = function(img, gallery){
    // init with an image in jquery form
    this.src = img.attr('src');

    this.setDimensions = function(){
        var temp_height, temp_width;
        $this = this;
        $this.image = $('<img />').attr('src', this.src).load(function(){
            $this.height = this.height;
            $this.width = this.width;
        });
    };

    // init functions
    this.setDimensions();
    gallery.images.push(this);


}; // Image

var Gallery = function(){
    this.width = $(window).width();
    this.height = $(window).height();
    this.images = [];

    this.createElements = function(){
        $('body').append('<div id="the_gallery"></div>');
        $('#the_gallery').css({width: this.width, height: this.height});
    };

    this.open = function(){
        this.createElements();

        var img = this.images[0];

        // see if the image is bigger than the window
        if( this.width >= img.width && this.height >= img.height) {
            console.log('image < window');
            // just show the image, centered
        }

        else {
            console.log('image > window');
            var temp_width, temp_height;
            // constrain to height
            if( img.width < img.height ) {
                console.log('image width < image height');
                temp_height = this.height;
                temp_width = (temp_height/img.height) * img.width;

                img.css({height:temp_height, width:temp_width});

            }

            // constrain to width
            else {
                console.log('image width > image height');
                temp_width = this.width;
                temp_height = (temp_width/img.width) * img.height;
                img.image.css({height:temp_height, width:temp_width});
            }

        }

        img.image.appendTo($('#the_gallery'));

    }; // open

    this.close = function(){
        $('#the_gallery').remove();
    }; // close
}; // Gallery

$(document).ready(function(){
    $('#gallery img').click(function(){
        launchGallery($(this));
    }); // click

    var gallery = new Gallery(),
        test = new Image($('#gallery img:first'), gallery);

    gallery.open();
}); // doc ready

【问题讨论】:

  • 这样我就可以提出更好的问题,为什么投反对票和投票结束?
  • "如果我 console.log 变量 img,我可以看到对象的属性和功能。但是,如果我 console.log(img.height);"
  • @pst 这是一个计时/异步问题,实际上正在发生。我也很疑惑。
  • 这可能与您的直接问题无关,但您应该重命名您的对象Image 是一个内置的宿主对象(DOM 0);覆盖它(你正在做的)可能会导致其他脚本中断。
  • @hookedonwinter 同样,该声明成立。首先为可靠诊断基础的地址将允许编写有用标题。我没有投反对票,但我也不会投赞成票。

标签: javascript oop object


【解决方案1】:

我投票赞成关闭,但这里有一些帮助:

当您调用new Image 时,您重新加载它并在load 事件触发时设置widthheight。那是 其余代码运行之后 - 当您到达第 55 行时,图像可能尚未加载。

一个简单的解决方案是直接从img.width() 获取宽度/高度,并始终在window.onload 上运行插件。如果您想支持动态加载的图像,您必须重新了解该逻辑,并等到所有内容都加载完毕后再构建图库。

另外,避免泄漏全局变量,您在 $this = this 中缺少 var(第 7 行)。

【讨论】:

  • 啊,没考虑时间问题。控制台表明问题出在其他地方(skitch.com/hookedonwinter/85ih1/index.html),但是,我肯定会更新它。我正在重新加载它,因为图像最初显示为缩略图 - 通过 css 而不是不同的图像 - 所以 .width() 只显示 100px。
  • 在 FF 中的抽查表明这绝对是时间问题。谢谢!
  • @hookedonwinter 这里是考虑到这个问题的快速重写:jsbin.com/ibudok/2
【解决方案2】:

对此我不确定:但值得一试。 而不是“$('#the_gallery').css({width: this.width, height: this.height});”

不妨试试这个:

"$('#the_gallery').css('width', this.width +'px').css('height', this.height +'px');"

【讨论】:

  • 那部分工作正常。它更低,带有 img.width 和 img.height
猜你喜欢
  • 1970-01-01
  • 2018-06-22
  • 2019-02-10
  • 2018-04-23
  • 2014-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-09
相关资源
最近更新 更多