If I have to create a domain model object in C# I might do something like this:
public class Person { Public string Name { get; set; } Public string Gender { get; set; } Public int Age { get; set; } }
In Javascript, what is the convention for defining such objects? I’m thinking something like this:
NAMESPACE.Person = function() { this.name = ''; this.gender = ''; this.age = 0; }
Yeah, spot on basically. The only thing to add is that you should prototype methods on. How you proto can be situational to the design, I generally prefer to do this internally to the class, but flagged so it only runs when required, and only the once.
Explaining why: The reason for this is performance and inheritance. Prototyped properties are directly associated with the class (function) object rather than the instance, so they are traversable from the function reference alone. And because they are on the class, only a single object needs to exist, not one per instance.