简体   繁体   English

JavaScript对象声明问题

[英]Javascript object declaration issue

I'm digging OOP for the first time but I've got a little problem: 我是第一次研究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)
}

But it looks like declaring this.img.height results in typeError . 但是看起来好像声明this.img.height导致typeError Can someone explain why? 有人可以解释为什么吗?

Also, how can I declare the method inside the object declaration? 另外,我怎么能申报对象申报里面的方法? Nothing special about it: I just don't want my code to look too messy. 没什么特别的:我只是不想让我的代码看起来太凌乱。

Is your object always static to the name panel? 您的对象在名称面板中是否始终是静态的? Then 然后

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

Is it not static but you don't want instances of it? 它不是静态的,但您不希望它的实例吗? Then make an initialisation function to get the correct this 然后,让一个初始化函数来得到正确的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();
...

Do you want to have multiple instances of the object? 您是否想要对象的多个实例? Then make a constructor 然后做一个构造函数

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(){
...

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

You are refering to different object even if thinking you are not doing that. 即使您认为自己没有这样做,您仍在引用其他对象。 So in your example, on third line this is refering current scope and not the object panel you are creating. 因此,在你的榜样,在第三行this是闯民宅目前的范围,而不是要创建的对象面板。 So do img and h 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)
}

Should be something like 应该是这样的

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);

It's because this will never be a reference to the object you're creating when using object literal syntax. 这是因为在使用对象字面量语法时, this绝不会引用您正在创建的对象

It's a reference to the outer variable scope. 它是对外部变量范围的引用。 To use literal syntax, you'll need to create the parts that do not require a self reference, then create rest after the initialization. 要使用文字语法,您需要创建不需要自引用的部分,然后在初始化后创建其余部分。

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;

I don't know what your h and prop variables are supposed to refer to. 我不知道您的hprop变量应该引用什么。

If you expect them to refer to members of the object, then you need to take those out as well. 如果希望他们引用该对象的成员,则也需要将其删除。 And the center variable just seems to come out of nowhere. 而且center变量似乎无处不在。

Seems like maybe you're just guessing at how JavaScript syntax works. 似乎您只是在猜测JavaScript语法是如何工作的。 If so, that's a hard way to learn. 如果是这样,那是一种很难学习的方法。 I'd recommend a basic tutorial before you continue. 在继续之前,我建议您先学习一下基础教程。

Panel object doesn't have panel.img.height property. 面板对象没有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 refers to the context in which panel is declared, not the panel object itself. this是指在其中声明panel的上下文,而不是面板对象本身。 A few workarounds : 一些解决方法:

Cache the referred object : 缓存引用的对象:

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

Create a blank panel object and add properties one by one : 创建一个空白面板对象,并逐一添加属性:

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

And methods can be declared like any other property. 方法可以像其他任何属性一样声明。

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