简体   繁体   English

JavaScript:如何分配与同一对象内的另一个变量相同的变量?

[英]JavaScript: How can I assign a variable the same as another variable inside the same object?

sorry if this is a duplicate, but I couldn't find any others.对不起,如果这是重复的,但我找不到其他人。

So I tried doing this.所以我尝试这样做。

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");    

var player1 = {
    width: 20,
    height: 75,
    x: canvas.width/6-player1.width/2,
    y: canvas.height/2-player1.height/2,
    speed: 5
};

function drawPlayer1() {
    ctx.beginPath();
    ctx.rect(player1.x, player1.y, player1.width, player1.height);
    ctx.fillStyle = "#b10000";
    ctx.fill();
    ctx.closePath();
}

drawPlayer1();

But the problem is that I can't assign x to player1.width because width is assigned inside player1 where it's getting "used".但问题是我不能将x分配给player1.width因为width是在player1内部player1 ,它被“使用”。

BTW I am doing this because of symmetry.顺便说一句,我这样做是因为对称。

I could just have these variables for themselves, but I am trying to clean up my code.我可以自己拥有这些变量,但我正在尝试清理我的代码。

So, how can I get around this problem by using objects?那么,如何通过使用对象来解决这个问题呢?

Consider using getters .考虑使用getter

 var player1 = { width: 20, height: 75, get x() { return this.width + 10 }, get y() { return this.height + 10 } }; console.log(player1.x, player1.y);

If you want the ability to set the value directly and override the formula, you could utilize setters too:如果您希望能够直接设置值并覆盖公式,您也可以使用setter

 var player1 = { width: 20, height: 75, _x: null, _y: null, get x() { return this._x || this.width + 10 }, set x (newX) { this._x = newX }, get y() { return this._y || this.height + 10 }, set y (newY) { this._y = newY } }; console.log(player1.x, player1.y); player1.x = 500; player1.y = 200; console.log(player1.x, player1.y);

Since player1.width is not defined yet-- because you are still in the middle of defining player1 -- you can define it and the other static properties first, and then assign the dynamic ones on the next line with Object.assign .由于player1.width尚未定义——因为您仍在定义player1的中间——您可以先定义它和其他静态属性,然后在下一行使用Object.assign分配动态属性。

var player1 = {
    width: 20,
    height: 75,
    speed: 5
};
Object.assign(player1, {
    x: canvas.width/6-player1.width/2,
    y: canvas.height/2-player1.height/2,
});

You can't access player1 from within the definition of player1 because it doesn't exist yet.您无法访问player1从定义范围内player1 ,因为它还不存在。 When the interpreter parses this code, it first creates an object out of the object literal, then it stores it in the player1 variable.当解释器解析这段代码时,它首先从对象字面量中创建一个对象,然后将它存储在player1变量中。 And since player1 didn't exist beforehand, player1.width causes an error.由于player1事先不存在, player1.width会导致错误。

// original code which doesn't work
var player1 = {
    width: 20,
    height: 75,
    x: canvas.width/6-player1.width/2,
    y: canvas.height/2-player1.height/2,
    speed: 5
};

A simple way to fix it is to set those variables after creating the object.修复它的一个简单方法是在创建对象后设置这些变量。

var player1 = { ... };

player1.x = canvas.width/6 - player1.width/2;
player1.y = canvas.height/2 - player1.height/2;

Alternatively you could do this:或者你可以这样做:

Object.assign(player1, {
    x: canvas.width/6 - player1.width/2,
    y: canvas.height/2 - player1.height/2;
});

This code creates a new object with the x and y properties and then copies them to player1 .此代码创建一个具有 x 和 y 属性的新对象,然后将它们复制到player1 But for just these two properties, I'd stick to the first solution, it's clearer and simpler.但对于这两个属性,我会坚持第一个解决方案,它更清晰、更简单。

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

相关问题 如何将Javascript变量分配给另一个变量的相同值 - How to assign a Javascript variable to the same value of another variable 为什么我不能在JavaScript中的同一if语句中分配然后检查变量? - Why can't I assign and then check a variable in the same if statement in Javascript? 如何在同一页面上使用相同的javascript变量? - How can I have the same javascript variable on the same page? 我可以将字符串分配给javascript对象变量吗? - Can I assign a string to a javascript object variable? 在同一页面上将Javascript变量分配给php变量 - Assign Javascript variable to php variable on the same page 为什么不能给具有相同名称的命名函数表达式中的变量赋值? - Why can’t I assign values to a variable inside a named function expression with the same name? JS:如何访问另一个函数内部的函数中的变量值,并且两个函数都属于同一对象? - JS: How may I access a variable value inside a function that's inside another function, and that both functions belong to the same object? 同一变量内的引用对象 - Reference object inside same variable 如何在JavaScript中递归调用函数时访问相同的变量? - How can I access the same variable in recursive call of a function in javascript? Javascript:分配一个变量并同时检查是否为假 - Javascript: Assign a variable and check if falsy at the same time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM