Every time the template is executed, a new object is created when it reaches that section. The same concept applies to any reference type, such as arrays, functions, and more.
Similar to objects created in JavaScript code, each call to a function like getObject
will result in a new object being returned.
function getObject() {
return {};
}
console.log(getObject() === getObject()); // false
When using objects in JavaScript, the equality is based on reference rather than the values contained in the objects. Vue utilizes a similar approach when checking for changed props, as Vue templates are compiled down to a render
function which is essentially plain JavaScript with object literals remaining as they are.
In the scenario mentioned, the render function for the parent component would generate a new object for prop1
each time it runs, assuming the template only includes the parent-child relationship:
function render(h) {
return h('child', {
props: {
prop1: {foo: 'bar'}
}
})
}
To avoid unnecessary child updates, it is recommended to store the object in the data
section and reference it by name to ensure the same object is utilized consistently.
The significance of this practice depends on the specific scenario, as extensive child updates can potentially impact performance. While child rendering typically doesn't require updating the DOM directly, computed properties, watches, or templates reliant on that prop may run if modified.