JSFiddle - React, Tailwind, and code Playground

by Samar Pattanayak

JavaScript

function Person(first, last, age, gender, interests) {
  this.name = {
    first,
    last
  };
  this.age = age;
  this.gender = gender;
  this.interests = interests;
};

Person.prototype.greeting = function() {
  alert('Hi! I\'m ' + this.name.first + '.');
};

//
function Teacher(first, last, age, gender, interests, subject) {
  Person.call(this, first, last, age, gender, interests);

  this.subject = subject;
}
//console.log("Person",Person)
var personobj= new Person("A","B",23,"Male","Love");


Teacher.prototype = Object.create(Person.prototype);//before this line only object properties and ethods(constructor function) are copied to Teacher , To inherit prototype of PERSON into TEACHER we have to write this line.
Teacher.prototype.constructor = Teacher;//in the prevoiUs line as we assigned PERSON prototype to TEACHER, TEACHER constructor property is now equal to Person(). THEN WE NEED to correctly assign it correctly using this line.

Teacher.prototype.greeting = function() {}

var teacherobj= new Teacher("A","B",23,"Male","Love","Physics");
console.log(Object.getOwnPropertyNames(Person));
console.log(Object.getOwnPropertyNames(Teacher));

console.log(Object.getOwnPropertyNames(Person.prototype));
console.log(Object.getOwnPropertyNames(Teacher.prototype));

console.log(teacherobj)
//console.log(Teacher.prototype.constructor)
//console.log(Object.getPrototypeOf(personobj));
//console.log(Person.prototype);

var a={
	id:1,
	name:2
}
var b =function(i){
	this.a=i;
}

//console.log(Object.getOwnPropertyNames(a));
//console.log(Object.getOwnPropertyNames(b));

var a1=Object.create(a);//Object.create with Object literal creates a prototype and stores all properties of "a" in newly created objcets prototype.
var b2=new b(99);//doesnot create a prototype.
var b1=Object.create(b,{
	i:{
		value:89
	}
});//
console.log(a1);
console.log(b2)
console.log(b1)
//The object used in Object.create actually forms the prototype of the new object, where as in the new Function()...