Skip to content

Instantly share code, notes, and snippets.

@nmaxcom
Last active February 2, 2018 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmaxcom/6185503fcfa0732031299cd31d716091 to your computer and use it in GitHub Desktop.
Save nmaxcom/6185503fcfa0732031299cd31d716091 to your computer and use it in GitHub Desktop.
-- Human.js -----------------------------------------------------
const Human = () => ({
name:'',
health: 100,
energy: 100,
hunger: 0,
age: 30,
changeProp: function(pair) {
this[Object.keys(pair)[0]] = Object.values(pair)[0];
},
getProp: function(key) {
return this[key];
},
});
module.exports = Human;
-- Scientist.js -----------------------------------------------------
const Human = require('./Human');
const maxAge = 60, minAge = 19;
const scientist = name => {
let obj = Object.assign({}, Human());
obj.changeProp({"age": Math.round(Math.random() * (maxAge - minAge) + minAge)});
obj.name = name;
let scienceObj = {
experience: 'skin cancer',
carrying: 'cellphone',
talk: function() {
console.log(`Well, my mane is ${this.name}, I'm ${this.age} years old and I know about ${this.experience}${this.carrying ? `. And I'm carry my ${this.carrying}` :'' }`);
},
};
return Object.assign({}, obj, scienceObj);
};
module.exports = scientist;
-- main.js -----------------------------------------------------
const scientist = require("./scientist");
const Joe = scientist('Joe Mazzini');
Joe.talk();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment