I am currently trying to create class inheritance using prototypes in javascript, and I understand how that works in the simple example below.
var Person = function(){
var name = "Steve";
}
var Employee = function(){
var company = "Stack Overflow";
}
Employee.prototype = new Person;
var employee1 = new Employee();
But now assume that I want to add parameters to the constructors so that I can set the values as I create the objects. So the updated code would look like this:
var Person = function(initName){
var name = initName;
}
var Employee = function(initName, initCompany){
//How can I use initName to set the value in the prototype class?
var company = initCompany;
}
Employee.prototype = new Person;
var employee1 = new Employee("Bob", "Intel");
How can I call into the Person constructor with the name? In C# (the language I'm most comfortable with), you would simply call the base constructor, and pass through the necessary parameters, but it doesn't seem like there is an equivalent pattern in javascript.
For reference, this is what it might look like in C#:
public class Person
{
string name;
public Person(string initName)
{
name = initName;
}
}
public class Employee : Person
{
string company;
public Employee(string initName, string initCompany) : base(initName)
{
company = initCompany;
}
}
var employee1 = new Employee("Bob", "Intel");
Disclaimer: As you can see, I am primarily a object oriented programmer, but I am currently working in javascript, so some of what I've asked may be non-sensical from a javascript perspective, feel free to challenge my presuppositions as necessary.