Created
August 29, 2015 22:43
-
-
Save xealgo/2aabd0abd7e9d900650b to your computer and use it in GitHub Desktop.
lodash / underscore - set default parameters while ignoring unknown vars.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Person = function(params) { | |
this.name = ""; | |
this.age = 0; | |
this.gender = "na"; | |
_.extend(this, _.pick(params, _.keys(this))); | |
}; | |
var jason = new Person({ | |
name: "Jason", | |
age: 29, | |
gender: "M", | |
occupation: "programmer" | |
}); | |
console.log(jason); | |
// produces: {name: "Jason", age: 29, gender: "M"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment