default
Specifies the default value for instances of the defined type.
default
Provide primitive values to act as the initial value for the property if no value is provided upon instantiation. For example:
import { ObservableObject } from "can/everything"
class Example extends ObservableObject {
static props = {
prop: {
default: "foo"
}
};
}
const ex = new Example();
console.log( ex.prop ); //-> "foo"
This could also be written using the Property shorthand method:
class Example extends ObservableObject {
static props = {
prop: "foo"
};
}
Parameters
- defaultVal
{*}
:The default value, which will be passed through setter and type.
Use
The following defaults age
to 0
and address
to an object:
import { ObservableObject } from "can/everything";
class Person extends ObservableObject {
static props = {
// A default age of `0`:
age: {
default: 0
}
};
}
const person = new Person();
console.log( person.age ); //-> 0
Other ways to set the default
There is a second way to provide a default value, which is explained in get default() and is useful when defining an object as a default.