I'm trying to get a handle on how prototypes work. I have this example:
function Person(name) {
if (arguments.length > 0)
this.init(name);
}
Person.prototype.init = function(name) {
this.name = name();
}
Employee.prototype = new Person();
function Employee (name, id) {
if (arguments.length > 0) {
this.init(name);
this.id = id;
}
}
var employee = new Employee("frank",01);
alert(employee.name);//returns frank
I'd trying to figure out how to combine the first two sections, and assign the "init" property within the function constructor. I have this, but it doesn't return anything:
function Person(name) {
if (arguments.length > 0)
this.init = function(name) {
this.name = name;
}
}
Employee.prototype = new Person();
function Employee (name, id) {
if (arguments.length > 0) {
this.init(name);
this.id = id;
}
}
var employee = new Employee("frank",01);
alert(employee.name);//returns nothing
I'm assuming I'm doing something wrong in the assigning of init, but I don't know what. Changing the initial property to
this.init(name) = function(name) {
doesn't work either.