DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Guides
  • Core
    • can-component
    • can-compute
    • can-connect
    • can-define
      • static
        • types
      • types
        • PropDefinition
      • behaviors
        • Type
        • Value
        • get
        • serialize
        • set
        • type
        • value
    • can-define/list/list
    • can-define/map/map
    • can-route
    • can-route-pushstate
    • can-set
    • can-stache
    • can-stache/helpers/route
    • can-stache-bindings
  • Ecosystem
  • Infrastructure
  • Legacy
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • CanJS
  • /
  • Core
  • /
  • can-define
  • / On this page
    • can-define

      module

      Exports the define method that defines observable properties and their behavior on a prototype object.

      • npm package badge
      • Star
      • source

      define(prototype, propDefinitions)

      Define observable properties, type conversion, and getter/setter logic on prototype objects.

      var define = require("can-define");
      
      var Greeting = function(message){
          this.message = message;
      };
      
      define(Greeting.prototype,{
          message: {type: "string"}
      });
      

      Parameters

      1. prototype {Object}:

        The prototype object of a constructor function or class. The prototype object will have getter/setters defined on it that carry out the defined behavior. The prototype will also contain all of can-event's methods.

      2. propDefinitions {Object<String,PropDefinition>}:

        An object of properties and their definitions.

      Use

      can-define provides a way to create custom types with observable properties. Where can-define/map/map and can-define/list/list provide more functionality, they also make more assumptions on the type constructor. can-define can be used to create completely customized types.

      The following creates a Person constructor function:

      var define = require("can-define");
      
      var Person = function(first, last){
        this.first = first;
        this.last = last;
      };
      define(Person.prototype,{
        first: { type: "string" },
        last: { type: "string" },
        fullName: {
          get: function(){
            return this.first+" "+this.last;
          }
        }
      });
      

      This can be used to create Person instances with observable properties:

      var person = new Person("Justin", "Meyer");
      person.first    //-> "Justin"
      person.last     //-> "Meyer"
      person.fullName //-> "Justin Meyer"
      
      person.on("fullName", function(ev, newVal, oldVal){
          newVal //-> "Ramiya Meyer"
          oldVal //-> "Justin Meyer"
      });
      
      person.first = "Ramiya"
      

      The observable properties call Observation.add so they can be observed by can-compute.

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