简体   繁体   English

对象和对象+属性的不同console.log属性值

[英]Different console.log property values on object and object + property

Iam currently playing around a bit with JavaScript and wanted to make a small canvas app. Iam目前正在使用JavaScript玩些游戏,并希望制作一个小型canvas应用程序。

My JavaScript code for this is 我的JavaScript代码是

"use strict";

const canvas = document.getElementById("canvas");
const context = canvas.getContext("2d");
let mouseDown = false;
let lastEvent;



canvas.onmousedown = function(e) {
    e.preventDefault();
    lastEvent = e;
    mouseDown = true;

    context.beginPath();
    context.lineWidth="5";
    context.strokeStyle="black";
    context.moveTo(e.offsetX,e.offsetY);
    context.lineTo(e.offsetX-4,e.offsetY-4);
    context.stroke();

    console.log(lastEvent);
    console.log(lastEvent.offsetX);
    console.log(e.offsetX);
    console.log(e);
};

canvas.onmousemove = function(e) {
    e.preventDefault();
    if (mouseDown) {
        context.beginPath();
        context.lineWidth="5";
        context.strokeStyle="black";
        context.moveTo(lastEvent.offsetX,lastEvent.offsetY);
        context.lineTo(e.offsetX,e.offsetY);
        context.stroke();
        lastEvent = e;
    };
};

canvas.onmouseup = function(e) {
    e.preventDefault();
    mouseDown = false;
};

As you can see the plan is: When the mouse moves on my canvas I draw a line by capturing the last mouse event and then drawing from there to the current mouse event. 如您所见,该计划是:当鼠标在画布上移动时,我通过捕获最后一个鼠标事件然后从那里绘制到当前鼠标事件来画一条线。 At last of course it doesnt work out as planned. 最后当然不能按计划进行。 For some reason the lastEvent.offsetX and .offsetY always turn out to be 0. 由于某种原因,lastEvent.offsetX和.offsetY总是变为0。

Now lets move to the onmousedown function, while the bug isnt caused here (atleast I hope) its where I tracked down whats going wrong and its confusing the hell out of me. 现在让我们转到onmousedown函数,而错误并不是在这里引起的(至少希望如此),它是我在哪里查找出了什么问题并将它弄得一团糟。 I have 4 console.log statements here and their output is the cause of the confusion. 我在这里有4个console.log语句,它们的输出是造成混乱的原因。

console.log(lastEvent) output is (when looking the offsetX up in the console) 0 console.log(lastEvent)输出为(在控制台中查找offsetX时)0

console.log(lastEvent.offsetX) output is (for example) 127 console.log(lastEvent.offsetX)输出为(例如)127

console.log(e.offsetX) output is also 127 console.log(e.offsetX)的输出也是127

console.log(e) output is (again looking offsetX up in the console) 0 console.log(e)输出为(再次在控制台中向上查找offsetX)0

and I completely dont understand how that is possible? 我完全不知道这怎么可能? When I call the object and then look the property value up its different when I directly ask for the value of the property?? 当我调用该对象然后直接查询属性值时,查找该属性值是否有所不同? Because of my understanding both values should be the same, as the object isnt changed anymore at this point. 根据我的理解,这两个值应该相同,因为此时对象不再更改。

I can think of a workaround to this problem, but what iam more curious about if someone could explain me please what is going on here? 我可以想出解决此问题的方法,但是我更好奇是否有人可以向我解释一下这是怎么回事?

The lastEvent object will be updated after your method is finish and console.log reflect the object it self, not making a copy of it. 方法完成后, lastEvent对象将更新,console.log会反映该对象本身,而不是对其进行复制。 You can test that by this simple snippet: 您可以通过以下简单代码段进行测试:

var myobj = { c : 1 };
console.log('my obj:', myobj);
console.log('c is', myobj.c);
myobj.c = 0;
//now check the logged object again.

For easier debugging, you can use Chrome Inspector or use debugger; 为了简化调试,您可以使用Chrome Inspector或debugger; to add a breakpoint in your code then feel free to inspect everything. 在代码中添加断点,然后随时检查所有内容。

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

相关问题 console.log()显示相同对象属性的矛盾值 - console.log() showing contradictory values for the same object property 为什么我的console.log显示不同的属性值,如果我使用相同的对象? reactjs - Why is my console.log showing different property values if I'm using the same object?? reactjs Javascript对象属性Console.log - Javascript Object Property Console.log 在 Metro console.log 中突出显示对象属性 - highlighting object property in metro console.log Javascript,console.log打印prints对象,但属性未定义 - Javascript, console.log prints prints object, but property is undefined 函数看不到 Object 属性,但 console.log 看不到 - Function doesn't see Object property, but console.log does Object 属性以点表示法调用,显示 console.log 'undefined' - Object property called with dot notation showing console.log 'undefined' javascript console.log在同一对象上显示不同的值 - javascript console.log displays different values on same object 为什么console.log()和调试器中的对象值不同? - Why object values are different in console.log() and in debugger? 即使对象上的console.log显示属性值,对象的属性也未定义 - Property of object is undefined, even though console.log on object is showing the property value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM