1

I am writing domain objects in Javascript which gets populated with the database fields. Suppose I have two objects dog and cat and I have following constructor function definition:

function Dog(opt_data) {
  var data = opt_data || {};

  this.createdAt = data['created_at'];
  this.updatedAt = data['updated_at'];
  this.name  = data['name'];
  this.breed = data['breed'];
}


function Cat(opt_data) {
  var data = opt_data || {};

  this.createdAt = data['created_at'];
  this.updatedAt = data['updated_at'];
  this.name = data['name'];
  this.fur  = data['fur'];
}

Now, both of the above objects have craetedAt and updatedAt properties. So, should I create a new class BaseModel which has there properties and let all the objects inherit that or is there any better alternative in javascript for this pattern?

Update 1:

My understanding from comments and answer.

function Cat(opt_data) {
  var data = opt_data || {};


  this.name = data['name'];
  this.fur  = data['fur'];

  this.updateTimestamp(data);
}
Cat.prototype = Object.create({
  updateTimestamp: function(data) {
    this.createdAt = data['created_at'] || new Date();
    this.updatedAt = data['updated_at'] || new Date();
  }
});
8
  • 1
    both of the above objects have craetedAt and updatedAt properties not in the code you posted. Commented Mar 9, 2017 at 4:38
  • Unless you find that you need something else, go for simple duck typing. Commented Mar 9, 2017 at 4:59
  • @Bergi can you please give me an example? Commented Mar 9, 2017 at 16:11
  • @CodeYogi You already did, the code you have is fine. If you don't think so, tell us what you need to do with your instances. Commented Mar 9, 2017 at 16:16
  • @Bergi you can see that I have a set of common properties which will be on every object I create don't you think its a repetitive code and prone to error? Commented Mar 9, 2017 at 16:20

1 Answer 1

1

Unless the createdAt and updatedAt values have some common supporting methods or accessors that you need to define on both Dog and Cat objects, just set the attributes to whatever value you need them to be.

Since you don't declare object members in JavaScript (the way you would in C++, C#, Java, etc.), there's nothing to be gained by inheriting from a BaseModel prototype in the case you have proposed. That is to say, since you don't have to do anything in JavaScript to create the createdAt and updatedAt attributes other than to simply assign to them, a base type does not really provide anything useful because you would just have to assign those attributes in the base type constructor anyway.

Where you may need a base type is if both objects need to have similar methods to save and load data (presumably automatically updating the updatedAt attribute when saving). In this case, giving both Dog and Cat a prototype with save and load methods would be a useful application of the prototypical inheritance pattern.

Sign up to request clarification or add additional context in comments.

6 Comments

What if there are more objects with the same attributes, don't you think we will be writing repetitive code?
@vivek That would be a case for a helper function that sets the attributes on an existing object. The prototypal inheritance model offers nothing useful for this specific case.
@cdhowie frankly speaking I am still unable to wrap my head around this, maybe I am not able to get out of the classical inheritance mode.
@CodeYogi That is a stumbling block for many developers trying to learn JavaScript. The part that you are likely hung up on is that in many languages with classical inheritance, you have to declare each property on a class. JavaScript does not have classes at all, it only has objects. You don't have to declare that a property exists, you just assign to it, and when you do, it springs into existence.
@cdhowie I don't remember but I have read somewhere defining properties in constructor function results in optimized code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.