0
我有一个对象生成器。它正常工作。如何正确更新对象?
'use strict';
function Div(isim) {
this.loc = document.getElementById(isim);
var style = window.getComputedStyle(this.loc);
this.width = style.getPropertyValue('width');
this.height = style.getPropertyValue('height');
this.left = style.getPropertyValue('left');
this.top = style.getPropertyValue('top');
}
但后来我更新的元素
var d = new Div("d");
d.loc.style.left = getRandomInt(0, window.innerWidth - 50) + "px";
d.loc.style.top = getRandomInt(0, window.innerHeight - 50) + "px";
console.log(d.left); //gives auto
console.log(d.width); //gives the right value
和console.log(d.left)
是错误的性质。我已经找到一种方法来解决它,但它是一个有点脏,我认为:
var d = new Div("d");
d.loc.style.left = getRandomInt(0, window.innerWidth - 50) + "px";
d.loc.style.top = getRandomInt(0, window.innerHeight - 50) + "px";
d = new Div("d");
console.log(d.left); //gives the right value
console.log(d.width); //gives the right value
是否有另一种方式(我更喜欢一个行)?不幸的是, 我不擅长英语,如果有问题的错误,标题,请编辑它们。