DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Guides
  • Core
  • Ecosystem
  • Infrastructure
    • can-construct
    • can-control
    • can-event
    • can-event/async/async
    • can-event/batch/batch
    • can-observation
    • can-simple-map
      • prototype
        • attr
    • can-util
    • can-view-callbacks
    • can-view-live
    • can-view-model
    • can-view-nodelist
    • can-view-parser
    • can-view-scope
    • can-view-target
  • Legacy
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • CanJS
  • /
  • Infrastructure
  • /
  • can-simple-map
  • /
  • attr
  • / On this page
    • attr

      function

      Get or set properties on a SimpleMap.

      • source

      map.attr(key)

      Reads a property from this SimpleMap.

      Parameters

      1. key {String}:

        the property to read

      Returns

      {*}:

      the value assigned to key.

      map.attr(key, value)

      Assigns value to a property on this SimpleMap called key.

      Parameters

      1. key {String}:

        the property to set

      2. the {*}:

        value to assign to key.

      Returns

      {can.SimpleMap}:

      this SimpleMap, for chaining

      map.attr(obj)

      Assigns each value in obj to a property on this SimpleMap named after the corresponding key in obj, effectively merging obj into the SimpleMap.

      Parameters

      1. obj {Object}:

        a collection of key-value pairs to set. If any properties already exist on the SimpleMap, they will be overwritten.

      Returns

      {can.SimpleMap}:

      this SimpleMap, for chaining

      Use

      attr gets or sets properties on the SimpleMap it's called on. Here's a tour through how all of its forms work:

      var map = new SimpleMap({ age: 29 });
      
      // get a property:
      foo.attr('age'); // 29
      
      // set a property:
      foo.attr('age', 30);
      foo.attr('age'); // 30
      
      // set and merge multiple properties:
      foo.attr({
          first: 'Kevin',
          last: 'Phillips'
      });
      foo.attr('age'); // 30
      foo.attr('first'); // 'Kevin'
      foo.attr('last'); // 'Phillips'
      

      When properties are changed using attr, the SimpleMap will emit events. Events can be listened to using on or bind.

      var map = new SimpleMap({ age: 29 });
      
      map.on('age', function(ev, newVal, oldVal) {
          newVal; // 30
          oldVal; // 29
      });
      
      map.attr('age', 30);
      

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