简体   繁体   English

Javascript对象:这两个值有何不同?

[英]Javascript objects: how are these two values different?

I'm pretty new to OOP, and I'm having trouble discerning why I am getting different values for the two console logs. 我是OOP的新手,我很难分辨为什么我要为两个控制台日志获取不同的值。 The first log gives me the correct height value, the second one which accesses the value through an object property is telling me the height is 0. I can also say that if I run a console log on imageLoader.images[this.sprite] , and on player1.height when this.height = imageLoader.images[this.sprite] (so basically I removed the .height property from each value), the first log correctly names the sprite even when it has been dynamically changed, while the second log once again is incorrect and only names the sprite that the object was assigned on creation (from the imageName argument). 第一个日志给我正确的高度值,第二个日志通过对象属性访问该值,告诉我高度为0。我也可以说,如果我在imageLoader.images[this.sprite]上运行控制台日志,在player1.heightthis.height = imageLoader.images[this.sprite] (因此基本上我从每个值中删除了.height属性)时,第一个日志正确命名了该精灵,即使它已经被动态更改了,而第二个日志再次记录是错误的,仅命名创建时已分配对象的精灵(通过imageName参数)。

As a little bit of background, the constructor is pulling images from the object images from within the object imageLoader , with each actual image contained in a property named after the image's name. 作为一点背景,构造函数正在从对象imageLoader内的对象图像中提取图像,每个实际图像都包含在以图像名称命名的属性中。 I want to have access to each image's height through the height property of each new Thing. 我想通过每个新Thing的height属性访问每个图像的高度。

function Thing(imageName) {
    this.sprite = imageName;
    this.height = imageLoader.images[this.sprite].height;
}

var player1 = new Thing ("face_right");

console.log(imageLoader.images[player1.sprite].height);
console.log(player1.height);

Hope that was somewhat clear. 希望这是明确的。 Thanks for taking a look. 谢谢参观。

You don't show any code that would cause the sprite to change, but you seem to be saying you want the player1.height to automatically update to reflect the height of whatever the current sprite is, where the sprite can change dynamically? 您没有显示任何会导致Sprite更改的代码,但是您似乎在说要让player1.height自动更新以反映当前Sprite的高度,Sprite可以动态更改? If so it won't work the way you currently have it because the this.height property you set will just be a primitive number value - it's not a reference to the other object's property. 如果是这样,它将无法以您当前拥有的方式工作,因为您设置的this.height属性将只是一个原始数字值-它不是对另一个对象属性的引用。 You'd need to add code to change the Thing instance's .height property when the sprite is updated. 您需要添加代码以在更新精灵时更改Thing实例的.height属性。

Or instead of having a .height property have a .height() method - or perhaps call it .getHeight() as follows: 或不具有.height属性,而具有.height()方法-或按如下方式将其.getHeight().getHeight()

function Thing(imageName) {
    this.sprite = imageName;
    this.getHeight = function() {
        return imageLoader.images[this.sprite].height;
    }
}

var player1 = new Thing ("face_right");
console.log(player1.getHeight());

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

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