For example, I already have this object somewhere in the code, it is a generic object:
var person1={lastName:\"Freeman\",firstName:\"Gordon\"};
No.
But if you're looking to treat your person1
object as if it were a Person
, you can call methods on Person
's prototype on person1
with call
:
Person.prototype.getFullNamePublic = function(){
return this.lastName + ' ' + this.firstName;
}
Person.prototype.getFullNamePublic.call(person1);
Though this obviously won't work for privileged methods created inside of the Person constructor—like your getFullName
method.
This is just a wrap up of Sayan Pal answer in a shorter form, ES5 style :
var Foo = function(){
this.bar = undefined;
this.buzz = undefined;
}
var foo = Object.assign(new Foo(),{
bar: "whatever",
buzz: "something else"
});
I like it because it is the closest to the very neat object initialisation in .Net:
var foo = new Foo()
{
bar: "whatever",
...
This borrows from a few other answers here but I thought it might help someone. If you define the following function on your custom object, then you have a factory function that you can pass a generic object into and it will return for you an instance of the class.
CustomObject.create = function (obj) {
var field = new CustomObject();
for (var prop in obj) {
if (field.hasOwnProperty(prop)) {
field[prop] = obj[prop];
}
}
return field;
}
Use like this
var typedObj = CustomObject.create(genericObj);
This is not exactly an answer, rather sharing my findings, and hopefully getting some critical argument for/against it, as specifically I am not aware how efficient it is.
I recently had a need to do this for my project. I did this using Object.assign, more precisely it is done something like this:Object.assign(new Person(...), anObjectLikePerson)
.
Here is link to my JSFiddle, and also the main part of the code:
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.getFullName = function() {
return this.lastName + ' ' + this.firstName;
}
}
var persons = [{
lastName: "Freeman",
firstName: "Gordon"
}, {
lastName: "Smith",
firstName: "John"
}];
var stronglyTypedPersons = [];
for (var i = 0; i < persons.length; i++) {
stronglyTypedPersons.push(Object.assign(new Person("", ""), persons[i]));
}
This worked for me. It's simple for simple objects.
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
getFullName() {
return this.lastName + " " + this.firstName;
}
static class(obj) {
return new Person(obj.firstName, obj.lastName);
}
}
var person1 = {
lastName: "Freeman",
firstName: "Gordon"
};
var gordon = Person.class(person1);
console.log(gordon.getFullName());
I was also searching for a simple solution, and this is what I came up with, based on all other answers and my research. Basically, class Person has another constructor, called 'class' which works with a generic object of the same 'format' as Person. I hope this might help somebody as well.
The answer of @PeterOlson may be worked back in the day but it looks like
Object.create
is changed. I would go for the copy-constructor way like @user166390 said in the comments.
The reason I necromanced this post is because I needed such implementation.
Nowadays we can use Object.assign (credits to @SayanPal solution) & ES6 syntax:
class Person {
constructor(obj) {
obj && Object.assign(this, obj);
}
getFullName() {
return `${this.lastName} ${this.firstName}`;
}
}
Usage:
const newPerson = new Person(person1)
newPerson.getFullName() // -> Freeman Gordon
ES5 answer below
function Person(obj) {
for(var prop in obj){
// for safety you can use the hasOwnProperty function
this[prop] = obj[prop];
}
}
Usage:
var newPerson = new Person(person1);
console.log(newPerson.getFullName()); // -> Freeman Gordon
Using a shorter version, 1.5 liner:
function Person(){
if(arguments[0]) for(var prop in arguments[0]) this[prop] = arguments[0][prop];
}