JSFiddle - React, Tailwind, and code Playground
by Sahil Batla
JavaScript
/*
* Javascript allows you to declare Classes
* e.g. we can use:
* var name = new String("First Last");
*
* Q1. create your own Human class.
* make it so that each instance of Human
* has a firstName, a lastName and a SSN
* These parameters need to be passed while creation of Human.
*
* Also make each Human instance have access to a method `greet`
* the greet method on any human should return
* "Hello, my name is {FirstName} {LastName}"
*/
//Write class definition here.
/* Remove this line after code is completed.
var human1 = Human("Sunny","Gupta", "CSGRHG345");
alert(human1.greet());
*/
/***************************************************************/
/*
* Inheritance
*
* Q2. Now create another class that inherits via Human,
* e.g. call it Indian.
* Indian should also have a state associated with him.
* The greet method on an Indian should return:
* "Hello, my name is {FirstName} {LastName}. I am from {StateName}"
*/
//Write Child Class here.
/* Remove this line after code completed./*
var indian1 = Indian("Sunny","Gupta","CSGRHG345", "Gujarat");
alert(indian1.greet());
/***************************************************************/
function Human(attributes) {
this.firstName = attributes.firstName;
this.lastName = attributes.lastName;
this.ssn = attributes.ssn;
}
Human.prototype.greet = function() {
return 'Hello, my name is ' + this.firstName + ' ' + this.lastName
}
var Indian = function(attributes) {
this.super = Human;
this.state = attributes.state;
delete attributes.state
this.super(attributes);
}
Indian.prototype = new Human({})
Indian.prototype.comnstructor = Indian
Indian.prototype.greet = function() {
return 'Hello, my name is ' + this.firstName + ' ' +
this.lastName + '.I am from ' + this.state
}
var attributes = { firstName: 'Sahil', lastName: 'Batla', ssn: '2332', state: 'Delhi' }
var human = new...