computed properties code example

Example 1: computed setter

// ...
computed: {
  fullName: {
    // getter
    get: function () {
      return this.firstName + ' ' + this.lastName
    },
    // setter
    set: function (newValue) {
      var names = newValue.split(' ')
      this.firstName = names[0]
      this.lastName = names[names.length - 1]
    }
  }
}
// ...

Example 2: js computed style

// window.getComputedStyle(<element>).<css-attribute>
// example :
window.getComputedStyle(document.getElementById("11")).width

Example 3: vue computed

var vm = new Vue({
  el: '#example',
  data: {
    message: 'Hello'
  },
  computed: {
    // a computed getter
    reversedMessage: function () {
      // `this` points to the vm instance
      return this.message.split('').reverse().join('')
    }
  }
})

Example 4: computed property in javascript

/*
Computed Property Names is ES6 feature which allows 
the names of object properties in JavaScript OBJECT LITERAL NOTATION
to be determined dynamically, i.e. computed.
*/

let propertyname = 'c';

let obj ={
	a : 11,
    b : 12,
    [propertyname] : 13
};

obj; // result is  {a:11 , b:12 , c:13}

//or incase if you want a as your object you can set in this way

let a_value = {
	[obj.a] = obj // a_value's key name as (a) and the complete (obj) present above itself will act as a value
};