DoneJS StealJS jQuery++ FuncUnit DocumentJS
6.0.1
5.33.2 4.3.0 3.14.1 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-bind
      • can-compute
      • can-debug
      • can-deep-observable
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-backup
      • can-define-stream
      • can-define-stream-kefir
      • can-event-queue
      • can-kefir
      • can-list
      • can-map
      • can-map-compat
      • can-map-define
      • can-observable-array
      • can-observable-object
        • behaviors
          • async
          • default
          • enumerable
          • get
          • get default()
          • identity
          • required
          • serialize
          • set
          • type
          • value
        • static
          • propertyDefaults
          • props
          • seal
        • prototype
          • get property()
          • set property()
        • types
          • DefinitionObject
          • Property
      • can-observation
      • can-observation-recorder
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
      • can-value
    • Views
      • can-attribute-observable
      • can-component
      • can-observable-bindings
      • can-stache
      • can-stache-bindings
      • can-stache-converters
      • can-stache-element
      • can-stache-route-helpers
      • can-view-autorender
      • can-view-callbacks
      • can-view-import
      • can-view-model
      • can-view-parser
      • can-view-scope
      • can-view-target
      • steal-stache
    • Data Modeling
      • can-connect
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-tag
      • can-define-realtime-rest-model
      • can-define-rest-model
      • can-fixture
      • can-fixture-socket
      • can-local-store
      • can-memory-store
      • can-ndjson-stream
      • can-query-logic
      • can-realtime-rest-model
      • can-rest-model
      • can-set-legacy
      • can-super-model
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-hash
      • can-route-mock
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-diff
      • can-globals
      • can-join-uris
      • can-key
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-queues
      • can-string
      • can-string-to-any
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-child-nodes
      • can-control
      • can-dom-data
      • can-dom-events
      • can-dom-mutate
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-fragment
    • Data Validation
      • can-define-validate-validatejs
      • can-type
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-data-types
      • can-namespace
      • can-reflect
      • can-reflect-dependencies
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

Property

  • Edit on GitHub

PrimitiveValue

Specify a property with a default primitive value. Using this signature will result in strict type checking being applied to this property.

import { ObservableObject } from "can/everything";

class Person extends ObservableObject {
  static props = {
    age: 72
  };
}

const me = new Person();
me.age; // -> 72

me.age = "45"; // throws

function()

Specify a property by defining its type as a primitive constructor. This can be String, Number, or Boolean. Using this signature results in strict type checking for this property.

import { ObservableObject } from "can/everything";

class Person extends ObservableObject {
  static props = {
    age: Number
  };
}

const me = new Person();

me.age = 75;
me.age; //-> 75

me.age = "45"; // throws

function()

Specify a property by defining its type as a constructor. This can be any constructor function, such as another ObservableObject, builtins like Date, or plain JavaScript constructor functions.

Specify a constructor function as a property's value will result in strict type checking for this property.

import { ObservableObject } from "can/everything";

class Occupation extends ObservableObject {}

class Person extends ObservableObject {
  static props = {
    occupation: Occupation,
    birthday: Date
  };
}

const me = new Person({
  birthday: new Date(1970, 2, 4),
  occupation: new Occupation()
});

me.birthday = "1970/3/19"; // throws

TypeObject

Specify a property as a special TypeObject. Most of the time you'll use this in conjunction with can-type to specify a type.

import { ObservableObject, type } from "can/everything";

class Person extends ObservableObject {
  static props = {
    first: type.check(String),
    last: type.check(String),
    middle: type.maybe(String)
  };
}

const me = new Person({
  first: "Michael",
  last: "Jordan"
});

console.log(me);

In the above example first and last use [can-type.check] which does strict type checking. middle uses [can-type.maybe] which accepts a String, null, or undefined.

These methods on can-type produce a TypeObject which looks like below:

const dateType = {
  [Symbol.for("can.new")](value) {
    return new Date(value);
  },
  [Symbol.for("can.isMember")](value) {
    return value instanceof Date;
  }
};

function()

Specify a property as a function sets the property to type: Function and uses the provided function as the default value. A use-case for this signature is to have a can-stache renderer as a default template while allowing consumers of your component to provide an alternative renderer.

import { ObservableObject, stache } from "can/everything";

// This is a function
const headerView = stache(`
  <header>
    <h1>{{title}} page</h1>
  </header>
`);

class ViewModel extends ObservableObject {
  static props = {
    header: headerView
  };
}

Returns

{undefined}:

DefinitionObject

Define a property through a DefinitionObject. This allows you define several different behaviors (as shown in the sidebar).

import { ObservableObject, type } from "can/everything";

class Person extends ObservableObject {
  static props = {
    age: {
      type: type.convert(Number),
      default: 1
    },
    birthday: {
      type: type.check(Date),
      set( newVal, current ) {
        if(newVal > new Date()) {
          console.warn('Birthday cannot be greater than today');
          return current;
        }
        return newVal;
      }
    }
  };
}

const me = new Person({
  age: 6
});

console.log(me);

CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 6.0.1.

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news