Skip to content

Instantly share code, notes, and snippets.

@dpedro
Last active August 11, 2016 14:33
Show Gist options
  • Save dpedro/084af38f23e08a582f95d3e7995e9867 to your computer and use it in GitHub Desktop.
Save dpedro/084af38f23e08a582f95d3e7995e9867 to your computer and use it in GitHub Desktop.
/*
JavaScript classes are introduced in ECMAScript 6 and are syntactical sugar over JavaScript's existing prototype-based inheritance.
The class syntax is not introducing a new object-oriented inheritance model to JavaScript. JavaScript classes provide a much simpler and clearer
syntax to create objects and deal with inheritance.
*/
var Users = class {
constructor(fullName, description) {
this.fullName = fullName;
this.description = description;
}
}
var CONFIG_USERS = { 'users' : {
"bob" : {
'fullName': 'Bob Dylan',
'description': 'Singer-songwriter'
},
"john" : {
'fullName': 'John Woo',
'description': 'Film director'
}
}
}
var usersObj = {} = CONFIG_USERS.users;
var myUsers = {};
Object.keys(usersObj).forEach(function(key,index) {
console.info("key", key); // key: the name of the object key
console.info("index", index); // index: the ordinal position of the key within the object
let userData = {} = Object.values(usersObj)[index];
console.info("userData", userData);
console.info("userData - fullName", userData["fullName"]);
myUsers[key] = new Users(userData["fullName"], userData["description"]) ;
});
console.info("--»", myUsers);
console.info("Bob", myUsers.bob);
console.info("Bob - fullName", myUsers.bob.fullName);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment