set property()
Specify a property's set behavior with the set syntax.
{
set propertyName( newValue ) { /* ... */ }
}
For example:
import { ObservableObject } from "can/everything";
class Person extends ObservableObject {
set fullName(newValue) {
const parts = newValue.split(" ");
this.first = parts[0];
this.last = parts[1];
}
}
const person = new Person( {fullName: "Justin Meyer"} );
console.log( person.first ); //-> "Justin"
console.log( person.last ); //-> "Meyer"
This is a shorthand for providing an object with a set
property like:
import { ObservableObject } from "can/everything";
class Person extends ObservableObject {
static props = {
fullName: {
set(newValue) {
const parts = newValue.split(" ");
this.first = parts[0];
this.last = parts[1];
}
}
};
}
const person = new Person( {fullName: "Justin Meyer"} );
console.log( person.first ); //-> "Justin"
console.log( person.last ); //-> "Meyer"