seal
Defines if instances of the map should be sealed in development.
Boolean
If true
, in development, instances of this object will be sealed. In strict mode errors will be thrown when undefined properties are set. This is the default behavior of extended DefineMaps:
"use strict"
import {DefineMap} from "can";
const Person = DefineMap.extend( {} );
const me = new Person();
try {
me.age = 33;
} catch(error) {
console.error( error.name + ": " + error.message ); //-> "TypeError: Cannot add property age, object is not extensible"
}
If false
, the object will not be sealed. This is the default behavior of
unextended DefineMaps. Use get and set to get and set values:
import {DefineMap} from "can";
const person = new DefineMap();
person.set( "first", "Justin" );
person.set( "last", "Meyer" );
console.log( person.get( "first" ) ); //-> "Justin"
console.log( person.get( "last" ) ); //-> "Meyer"
It is also possible to extend a sealed object and unseal it:
"use strict"
import {DefineMap} from "can";
const Person = DefineMap.extend( {} );
const Programmer = Person.extend( { seal: false }, {} );
const me = new Programmer();
try {
me.age = 33; // no error thrown
} catch(error) {
console.error( error.name + ": " + error.message );
}
Set seal
to false
on objects that have an indeterminate number of properties:
import {DefineMap} from "can";
import "//unpkg.com/underscore@1/underscore-min.js";
const Style = DefineMap.extend( {
seal: false
}, {
cssText: {
get: function() {
return _.map( this.get(), ( val, prop ) => {
return prop + ": " + val + ";";
} ).join( " " );
}
}
} );
const style = new Style();
style.set( "color", "green" );
style.set( "font", "awesome" );
console.log( style.cssText ); //-> "color:green; font: awesome;"