Reputation: 6648
I'm trying to create a new object by using an already made object. This is what I am trying to achieve:
var obj = {"Name" : "Patrick", "Age": 25, "Country": "US"};
document.writeln(JSON.stringify(obj) + "<br />");
function Person(name, age, country) {
this.name = name;
this.age = age;
this.country = country;
}
document.writeln(JSON.stringify(new Person(obj)));
https://jsfiddle.net/dotjz9tb/
As you can see, I am trying to create a new person called Patrick, aged 25, who happens to live in the US. My Person
constructor takes a name, an age, and a country as parameters. However, my object has already been created from a previous function, so I would like to do new Person(obj)
and it would create a new object based on obj
's parameters.
I know I can do new Person(obj.name, obj.age, obj.country)
but I would rather pass the constructor an object instead, while still being able to do new Person("Jack", 52, "UK")
.
Is such thing possible? Right now this is how it's really being created:
new Person(obj, undefined, undefined);
//or
{
"name": {
"Name":" Patrick","
Age":25,
"Country":"US"
}
}
Meaning that age
and country
is set to nothing, while name
is set to my object.
Upvotes: 2
Views: 359
Reputation: 45121
You have plenty of options here.
For example you could make your Person
polymorphic on arguments it takes.
function Person(name, age, country) {
if(arguments.length === 1 && typeof name === 'object') {
var data = arguments[0];
name = data.name; //etc
}
//the rest of ctor code goes here
}
Or BETTER use factory function and keep your constructor code as simple is possible
function copyPerson(obj) {
return new Person(obj.name, obj.age, obj.country);
}
//or even
Person.copy = function(obj) {
return new this(obj.name, obj.age, obj.country)
}
Upvotes: 1
Reputation:
In general it's better to avoid polymorphic interfaces. They end up being hard to maintain and document and remember.
In this case, I'd change the signature of the constructor to take an object, so
function Person(data) {
Object.assign(this, data);
}
Now you can call it as
var patrick = new Person({name: 'Patrick', age: 25, country: 'US'});
var patrick2 = new Person(patrick);
Upvotes: 1
Reputation: 5926
var Person = function(nameOrObj, age, country) {
if (typeof nameOrObj === 'object') {
this.name = nameOrObj.name
this.age = nameOrObj.age
this.country = nameOrObj.country
} else {
this.name = nameOrObj
this.age = age
this.country = country
}
}
Upvotes: 0
Reputation: 956
This is what you could do to support both the input formats:
function Person(name, age, country) {
var obj;
if(typeof(name) === 'object'){
obj = name;
name = obj.name;
age = obj.age;
country = obj.country;
}
this.name = name;
this.age = age;
this.country = country;
}
Upvotes: 0