jeandremelaria
4/1/2018 - 4:40 PM

ReactJs - Object initializer

shorthand property syntax to initialize your objects more concisely.

// ES 5 
const name = 'Robin';

const user = {
	name: name,
};

// ES 6 
const name = 'Robin';

const user = {
	name,
};

// ES 5 
this.state = {
	list:list,
};

// ES 6 
this.state = {
	list,
};

// ES 5 
var userService = {
	getUserName: function(user) {
		return user.firstname + ' ' + user.lastname;
	}
};

// ES 6
const userService = {
	getUserName(user) {
		return user.firstname + ' ' + user.lastname;
	}
}

// ES 5
var user = {
	name:'Robin'
};

// ES 6 
const key = 'name';
const user = {
	[key]: 'Robin'
};