繁体   English   中英

JavaScript对象声明问题

[英]Javascript object declaration issue

我是第一次研究OOP,但有一个小问题:

var panel = {    
    img : imgs[24],
    prop : this.img.height / this.img.width,

    h : this.img.height - (scale),
    w : h/prop,

    x : center.x - (w / 2),
    y : center.y - (h / 2)    
}

panel.draw = function(){    
   g.ctx.drawImage(this.img,
      0, 0,
      this.img.width, this.img.height,
      this.x, this.y,
      this.w, this.h)
}

但是看起来好像声明this.img.height导致typeError 有人可以解释为什么吗?

另外,我怎么能申报对象申报里面的方法? 没什么特别的:我只是不想让我的代码看起来太凌乱。

您的对象在名称面板中是否始终是静态的? 然后

var panel = {};
panel.img = imgs[24];
panel.prop = panel.img.height / panel.img.width;
...

它不是静态的,但您不希望它的实例吗? 然后,让一个初始化函数来得到正确的this

var panel = {   // assuming "scale" and "center" in scope
        init : function(){
            this.img = imgs[24];
            this.prop = this.img.height / this.img.width;
            this.h = this.img.height - (scale);
            this.w = this.h / this.prop;
            this.x = center.x - (this.w / 2);
            this.y = center.y - (this.h / 2);
        }
};
panel.init();
...

您是否想要对象的多个实例? 然后做一个构造函数

function Panel (img) { // assuming "scale" and "center" in scope
    this.img = img;
    this.prop = img.height / img.width;
    this.h = img.height - (scale);
    this.w = this.h / this.prop;
    this.x = center.x - (this.w / 2);
    this.y = center.y - (this.h / 2);
}
Panel..draw = function(){
...

并与var panel = new Panel( imgs[24] );

即使您认为自己没有这样做,您仍在引用其他对象。 因此,在你的榜样,在第三行this是闯民宅目前的范围,而不是要创建的对象面板。 imgh

var panel = {    
    img : imgs[24],
    prop : this.img.height / this.img.width, // this != panel here

    h : this.img.height - (scale), // this.img != panel.img
    w : h/prop, // h != panel.h

    x : center.x - (w / 2), // w != panel.w
    y : center.y - (h / 2)  // h != panel.h  
}

panel.draw = function(){    
   g.ctx.drawImage(this.img,
      0, 0,
      this.img.width, this.img.height,
      this.x, this.y,
      this.w, this.h)
}

应该是这样的

var Panel = (function() {    
    function Panel(img, scale, center) {  
       this.img = img
       this.prop = img.height / img.width
       this.h = img.height - scale
       this.w = this.h/this.prop
       this.x = center.x - (this.w / 2),
       this.y = center.y - (this.h / 2)
    }
    Panel.prototype.draw = function(ctx) {
          ctx.drawImage(this.img, 0, 0,
          this.img.width, this.img.height,
          this.x, this.y,this.w, this.h)
    }          
})():

var panel = new Panel(imgs[24], scale, center);
panel.draw(g.ctx);

这是因为在使用对象字面量语法时, this绝不会引用您正在创建的对象

它是对外部变量范围的引用。 要使用文字语法,您需要创建不需要自引用的部分,然后在初始化后创建其余部分。

var panel = {    
    img : imgs[24],

    w : h/prop,

    x : center.x - (w / 2),
    y : center.y - (h / 2)    
};
panel.prop = panel.img.height / panel.img.width;
panel.h = panel.img.height - scale;

我不知道您的hprop变量应该引用什么。

如果希望他们引用该对象的成员,则也需要将其删除。 而且center变量似乎无处不在。

似乎您只是在猜测JavaScript语法是如何工作的。 如果是这样,那是一种很难学习的方法。 在继续之前,我建议您先学习一下基础教程。

面板对象没有panel.img.height属性。

var panel = {    
    img :
        { height: // stuff here
        }
    prop : this.img.height / this.img.width,

    h : this.img.height - (scale),
    w : h/prop,

    x : center.x - (w / 2),
    y : center.y - (h / 2)    
}

this是指在其中声明panel的上下文,而不是面板对象本身。 一些解决方法:

缓存引用的对象:

var img = imgs[24];
var panel = {
    img: img
  , height: img.height - scale
  , //...

创建一个空白面板对象,并逐一添加属性:

var panel = {};
panel.img = imgs[24]
panel.height = panel.img - scale;

方法可以像其他任何属性一样声明。

var panel = {
    img: imgs[24]
  , draw: function(){ g.ctx.drawImage(this.img, 0, 0) }
}

暂无
暂无

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

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