Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
brandonlind2
7,823 PointsIn the composition pattern is there a way to inherit only specific properties of a source object?
for example
// with Object.assign I can inherit from multiple objects
let fish={
laysEggs: true,
habitat: "water"
};
let dog={
breaths: "air",
habitat:" land"
};
let newt=Object.assign({},dog,fish);
/*note I can have fish.habitat be the thing newt inherit
s by placing last so it over rides the dog.habitat*/
//but what if I want specific properties from each
/* what if the properties I needed weren't set up in a nice order
that allowed the ones I didnt want to be overridden */
//for example if they had the same property names with different values this wouldnt work
let mammal={
births:"live",
has: "hair",
metabolism: "warm blooded"
};
let reptile={
births: "eggs",
has:"scales",
metabolism: "cold blooded"
};
let platypus=Object.assign({},mammal,reptile);
let platypus2=Object.assign({},reptile,mammal);
//or if both had one property I didnt want
let mammal2={
births:"live",
has: "hair",
metabolism: "warm blooded"
};
let reptile2={
births: "eggs",
has: "scales",
metabolism: "cold blooded"
};
let platypus3=Object.assign({},mammal2,reptile2);
let platypus4=Object.assign({},reptile2,mammal2);
/*no matter which one I put first, I'm either going to have a platypus
with scales or one that gives live births*/
/*the only way to avoid this would be to be able to select which
properties from each object, you want or delete the properties
you dont want after creating the object.*/
/*So my questions is there a way to select which properties
you from each object and leave other out, when combining objects?*/