While there are numerous versions of this query, the existing answers fail to provide in-depth analysis.
Query: How does this result in a height of 200 pixels?
Consider the following code snippet:
var el = document.querySelector('#r');
console.log('First:: ' + el.offsetHeight);
el.style = {
height: el.offsetHeight + 500 + 'px'
}
console.log('Second:: ' + el.offsetHeight);
<div id="r" style="height:200px;width:800px;overflow:auto;border:1px solid;margin:20px;box-sizing:border-box"></div>
I suspect el.style
is read-only, leading me to anticipate that setting an object will fail silently, resulting in an output of:
First:: 200, Second:: 200
However, the actual outputs are:
First:: 200, Second:: 0
Why is this the case?
Query: Why does using Object.assign to set el.style work?
Object.assign(el.style,{
height : el.offsetHeight + 500
})
Could someone offer a more detailed explanation?