prototypal inheritance javascript - javascript

I am getting below error while using constructor function to implement prototypal inheritance in JavaScript.
I am trying to achieve custom (user defined) prototype chain like this -
function Human() {
this.species = "Homo Sapiens";
}
Human.prototype.run = function () {
console.log("Running...");
};
function Person(gender, age, name) {
Human.call(this);
this.name = name;
this.gender = gender;
this.age = age;
}
Person.prototype.printAge = function() {
console.log(this.age);
}
Person.prototype.printGender = function() {
console.log(this.gender);
}
function Employee(gender, age, name, dept, salary) {
Person.call(this, gender, age, name);
this.department = dept;
this.salary = salary;
}
Person.prototype = Object.create(Human.prototype);
Person.prototype.constructor = Person;
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;
const employee = new Employee("female", 28, "Radha", "Manager", 50000);
employee.run();
employee.printAge();
Uncaught TypeError: employee.printAge is not a function

See mdn docs to learn why it's not working
Better use:
Object.setPrototypeOf(
Derived.prototype,
Base.prototype,
);
function Human() {
this.species = "Homo Sapiens";
}
Human.prototype.run = function () {
console.log("Running...");
};
function Person(gender, age, name) {
Human.call(this);
this.empname = name;
this.gender = gender;
this.age = age;
}
Person.prototype.printAge = function() {
console.log(this.age);
}
Person.prototype.printGender = function() {
console.log(this.gender);
}
function Employee(gender, age, name, dept, salary) {
Person.call(this, gender, age, name);
this.department = dept;
this.salary = salary;
}
Object.setPrototypeOf(
Person.prototype,
Human.prototype,
);
Object.setPrototypeOf(
Employee.prototype,
Person.prototype,
);
const employee = new Employee("female", 28, "Radha", "Manager", 50000);
employee.run();
employee.printAge();

Related

the incrementAge function is returning undefined/NaN when invoked after defining the new User

sorry, the incrementAge function is returning undefined/NaN when invoked after defining the new User. I am not sure what's wrong
function User(name,age){
this.name = name;
this.age = age;
}
User.prototype.incrementAge = ()=>{
return this.age++;
}
const mike = new User("Mike",20);
console.log(mike.incrementAge());
The correct way to do this is to create a User class and create a method to raise the value of the variable age.
As you can see by calling the increment age method several times the value is added.
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
incrementAge() {
return ++this.age;
}
}
const mike = new User("Mike", 20);
console.log(mike.incrementAge());
console.log(mike.incrementAge());
The solution (by #ChrisG in comments)
function User(name, age) {
this.name = name;
this.age = age;
}
User.prototype.incrementAge = function () {
return ++this.age;
}
const mike = new User("Mike", 20);
console.log(mike.incrementAge());

Output not correct, unsure of how to fix

I have to have two arguements, name and dept, and made a name and dept instance variable that defaults to name unknown and department unknown, then make the get and set methods for them, but each time I run this it gives me the class name and dept is undefined
Well originally I didn't have it in a class and had it as a straight const function, and it was working, but when I couldn't reference it properly in another file, I was told they need to be put in a class which I tried, and now its not giving the output I need.
class Faculty {
constructor(name, dept) {
this.name = "name unknown";
this.dept = "department unknown";
}
getName() {
return this.name;
}
getDept() {
return this.dept;
}
setName(name) {
this.name = name;
}
setDept(dept) {
this.dept = dept;
}
}
Faculty.toString = function () {
return this.name.concat(" of ").concat(this.dept);
}
//Faculty.name = "Testname";
//Faculty.dept = "Electronic Technology";
console.log(Faculty.toString());
When ran it gives Faculty of undefined, even when I try to define name, it still just says Faculty, though I need it to be of that defaults to name unknown of department unknown unless set otherwise.
Here's how I would put it (and it works)
EDIT : Since this answer was chosen, I will add here the good elements pointed by the other answers
const DEFAULT_NAME = "name unknown";
const DEFAULT_DEPT = "department unknown";
class Faculty {
constructor(name = DEFAULT_NAME, dept DEFAULT_DEPT) {
this.name = name;
this.dept = dept;
}
getName() {
return this.name;
}
getDept() {
return this.dept;
}
setName(name) {
this.name = name;
}
setDept(dept) {
this.dept = dept;
}
toString() {
return `${this.name} of ${this.dept}`;
}
}
const f = new Faculty("Faculty", "Department");
console.log(f.toString());
Also you can use the default params like this :
class Faculty {
constructor(name = 'name unknown', dept = 'department unknown') {
this.name = name;
this.dept = dept;
}
getName() {
return this.name;
}
getDept() {
return this.dept;
}
setName(name) {
this.name = name;
}
setDept(dept) {
this.dept = dept;
}
toString() {
return `${this.name} of ${this.dept}`;
}
}
const f = new Faculty('Alex', 'Maths');
console.log(f.toString());
For one, you'd have to create a new instance of Faculty in order to call one of its class methods.
Second, there's no need to declare the toString method outside of the class; it can be included just as the others.
Third, I think the method itself could be simplified/clarified by using template literals.
const DEFAULT_NAME = "name_unknown";
const DEFAULT_DEPARTMENT = "department_unknown";
class Faculty {
constructor(name, dept) {
this.name = name || DEFAULT_NAME;
this.dept = dept || DEFAULT_DEPARTMENT;
}
getName() {
return this.name;
}
getDept() {
return this.dept;
}
setName(name) {
this.name = name;
}
setDept(dept) {
this.dept = dept;
}
toString() {
return `${this.name} of ${this.dept}`
}
}
//With name and department
const faculty = new Faculty("John Smith", "Department XYZ");
console.log(faculty.toString());
//Without name and department
const faculty_default = new Faculty();
console.log(faculty_default.toString());

Inherit function in javascript

I have a function that is similar to each other. How can I make declaring a function easier without duplicating
function constructor (name, numLegs) {
this.name = name;
this.numLegs = numLegs;
}
function Animal(name, numLegs) {
this.name = name;
this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
console.log("Hi my name is " + this.name);
};
// create a Penguin constructor here
function Penguin(name, numLegs){
this.name=name;
this.numLegs = numLegs;
}
// create a sayName method for Penguins here
Penguin.prototype.sayName = function() {
console.log("Hi my name is " + this.name);
};
// our test code
var theCaptain = new Penguin("Captain Cook", 2);
theCaptain.sayName();
You were almost there.
// create a Penguin constructor here
function Penguin(name, numLegs){
Animal.call(this, name, numLegs);
};
// Reuse the prototype chain
Penguin.prototype = Object.create(Animal.prototype);
Penguin.prototype.constructor = Penguin;

javascript prototyping, what am i doing wrong here

http://jsfiddle.net/johnm01/c5k0yevm/
im trying to understand prototyping again but not sure what im doing wrong here, why is undefined output when the method screamName is called?
function Person1(name, age, sex) {
this.name = name;
this.age = age;
this.sex = sex;
this.sayName = function sayMyName() {
alert(this.name + this.age + this.sex);
}
};
var p1 = new Person1("tim", 56, "male");
p1.sayName();
Person1.prototype.screamName = function screamName(name) {
this.name = name;
alert(this.name);
};
p1.screamName();
The screamName function expects a name argument which you're not supplying, and subsequently are assigning to this.name. Thus, it is undefined. I believe you intended to write this:
Person1.prototype.screamName = function screamName() {
alert(this.name);
};
Well, it looks like you've defined your method to take in a parameter of "name", but you haven't actually passed any parameter into the method when you call it. I also think that you need to create a new Person1 object in order to have a Person1 object with the screamname method.
function Person1(name, age, sex) {
this.name = name;
this.age = age;
this.sex = sex;
this.sayName = function sayMyName() {
alert(this.name + this.age + this.sex);
}
}
var p1 = new Person1("tim", 56, "male");
p1.sayName();
Person1.prototype.screamName = function screamName() {
alert(this.name);
};
var p2 = new Person1("bob", 22, "male");
p2.screamName();
https://jsfiddle.net/johnm01/c5k0yevm/

How do I create a class in javascript

I need a student class in javascript with 2 data members Name and Age and 2 method get_record() and set_record(name,age). How do I do it in javascript and create multiple object of that class.
var Student = function(age, name){
this.age = age;
this.name = name;
this.get_age = function(){
return this.age;
}
this.get_name = function(){
return this.name;
}
this.set_age = function(age){
this.age = age;
}
this.set_name = function(name){
this.name = name;
}
}
var student = new Student(20,"XYZ");
You can model classes with new JavaScript based languages. Dart and TypeScript are probably the most popular in this respect.
This example is based on the JavaScript output from a TypeScript class.
var Student = (function() {
function Student(name, age) {
this.name = name;
this.age = age;
}
Student.prototype.get_record = function() {
return "Name: " + this.name + "\nAge: " + this.age;
}
Student.prototype.set_record = function(name, age) {
this.name = name;
this.age = age;
}
return Student;
})();
// Usage
var a = new Student("John", 23);
var b = new Student("Joe", 12);
var c = new Student("Joan", 44);
function student (age,name) {
this.name = name;
this.age = age;
this.get_record = function() {
return "name:"+this.name+" , age:"+this.age;
}
this.set_record = function(_name,_age) {
this.name=_name;
this.age=_age;
}
}
You can use 'constructor function'.
function Student() {
this.get_record = function(){ return this.name; };
this.set_record = function(name, age) {
this.name = name;
this.age = age;
};
return this;
}
var student1 = new Student();
var student2 = new Student();
student1.set_record('Mike', 30);
student2.set_record('Jane', 30);
student1.get_record();
student2.get_record();
More complex class structures are constructed via prototypes

Categories