DoneJS StealJS jQuery++ FuncUnit DocumentJS
5.33.3
6.0.0 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-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-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-live
      • can-view-model
      • can-view-nodelist
      • 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-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

get

  • Edit on GitHub

Specify what happens when a certain property is read on an object. get functions are computed and automatically update themselves when a dependent observable value is changed.

get( [lastSetValue] )

Defines the behavior when a property value is read on a instance. Used to provide properties that derive their value from other properties on the object, or the property value that was set on the object.

Specify get like:

define = {
  propertyName: {
    get() { /* ... */ }
  },
  propertyName: {
    get( lastSetValue ) { /* ... */ }
  }
}

get propertyName() {

}

Parameters

  1. lastSetValue {*}:

    The value last set by instance.propertyName = value. Typically, lastSetValue should be an observable value, like a can-simple-observable or promise. If it's not, it's likely that a set should be used instead.

Returns

{*}:

The value of the property.

Use

Getter methods are useful for:

  • Defining virtual properties on a map.
  • Defining property values that change with their internal set value.

Virtual properties

Virtual properties are properties that don't actually store any value, but derive their value from some other properties on the map.

Whenever a getter is provided, it is wrapped in a can-observation, which ensures that whenever its dependent properties change, a change event will fire for this property also.

import { ObservableObject } from "can";

class Person extends ObservableObject {
  static props = {
    first: String,
    last: String
  };

  get fullName() {
    return this.first + " " + this.last;
  }
}

const p = new Person( { first: "Justin", last: "Meyer" } );

console.log(p.fullName); //-> "Justin Meyer"

p.on( "fullName", function( ev, newVal ) {
    console.log(newVal); //-> "Lincoln Meyer";
} );

p.first = "Lincoln";

Properties values that change with their internal set value

A getter can be used to derive a value from a set value. A getter's lastSetValue argument is the last value set by instance.propertyName = value.

For example, a property might be set to a observable, but when read, provides the value of the observable.

import { ObservableObject, SimpleObservable, Reflect } from "can";

class MyMap extends ObservableObject {
  static props = {
    value: {
      get( lastSetValue ) {
        return lastSetValue.value;
      }
    }
  };
}

const map = new MyMap();
const observable = new SimpleObservable( 1 );
map.value = observable;

console.log(map.value); //-> 1
Reflect.setValue(observable, 2);
console.log(map.value); //-> 2

This technique should only be used when the lastSetValue is some form of observable, that when it changes, can update the getter value.

For simple conversions, set or type should be used.

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

On this page

Get help

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