Object or array defaults must be returned from a factory function (source).
Example:
foobar: {
type: Object,
default() {
return {}
}
}
// OR
foobar: {
type: Object,
default: () => ({})
}
foobar
will return an empty object {}
by default.
But if you want to know if it is defined, it will always return "true". So you can do this instead:
foobar: {
type: Object,
default() {}
}
// OR
foobar: {
type: Object,
default: () => {}
}
foobar
will return as undefined
by default.
Is that last practice naughty?
2
Answers
As @EstusFlask comments; instead of it invoking an empty function (
default: () => {}
i.e.(() => {})()
) that returnsundefined
, you might as well just assign the default asundefined
ornull
like so:foobar
will return asnull
by default.You can check an object for definition by counting its keys:
Or by using JSON’s stringify method:
There are many other approaches to check for an object’s emptiness. The reference topic.