First, there are many duplicates for this question but those answers don't give deeper insights.
Q1. Why this results in 200,0 ?
Consider this 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
to be read-only , so I expect setting an object should silently fail and therefore I expect the output to be
First:: 200,Second:: 200
but it is:
First:: 200,Second:: 0
Why ?
Q2. Why setting el.style using Object.assign works ?
Object.assign(el.style,{
height : el.offsetHeight + 500
})
Can someone please explain me with some deeper insights?