Skip to content

Instantly share code, notes, and snippets.

@eshrinivasan
Last active January 11, 2022 06:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eshrinivasan/0f6d38562a65fe2a7fe40606ce1d7940 to your computer and use it in GitHub Desktop.
Save eshrinivasan/0f6d38562a65fe2a7fe40606ce1d7940 to your computer and use it in GitHub Desktop.
Interview code check 2022
function Human(name, age){
this.name = name;
this.age = age;
}
Human.prototype.goestoWork = function(){
return true;
}
function Employee(name, age, empid, gender){
Human.call(this, name, age);
this.empId = empid;
this.gender = gender;
}
Employee.prototype = Object.create(Human.prototype);
Employee.constructor = Employee;
Employee.prototype.finishesProject = function(){
return true;
}
function Avatar(name, age, empid, gender){
Employee.call(this, name, age, empid, gender);
this.canbecomeDad = function(){
return true;
}
}
Avatar.prototype = Object.create(Employee.prototype);
Avatar.constructor = Avatar;
var senthil = new Avatar("Senth", '40', "A123", "M" );
console.log(senthil.empId);
console.log(senthil.gender);
console.log(senthil.name);
console.log(senthil.canbecomeDad());
console.log(senthil.age);
console.log(senthil.goestoWork());
console.log(senthil.finishesProject());
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
function Human(name, age){
this.name = name;
this.age = age;
}
Human.prototype.goestoWork = function(){
return true;
}
function Employee(name, age, empid, gender){
Human.call(this, name, age);
this.empId = empid;
this.gender = gender;
}
Employee.prototype = Object.create(Human.prototype);
Employee.constructor = Employee;
Employee.prototype.finishesProject = function(){
return true;
}
function Avatar(name, age, empid, gender){
Employee.call(this, name, age, empid, gender);
this.canbecomeDad = function(){
return true;
}
}
Avatar.prototype = Object.create(Employee.prototype);
Avatar.constructor = Avatar;
var senthil = new Avatar("Senth", '40', "A123", "M" );
console.log(senthil.empId);
console.log(senthil.gender);
console.log(senthil.name);
console.log(senthil.canbecomeDad());
console.log(senthil.age);
console.log(senthil.goestoWork());
console.log(senthil.finishesProject());
</script>
<script id="jsbin-source-javascript" type="text/javascript">function Human(name, age){
this.name = name;
this.age = age;
}
Human.prototype.goestoWork = function(){
return true;
}
function Employee(name, age, empid, gender){
Human.call(this, name, age);
this.empId = empid;
this.gender = gender;
}
Employee.prototype = Object.create(Human.prototype);
Employee.constructor = Employee;
Employee.prototype.finishesProject = function(){
return true;
}
function Avatar(name, age, empid, gender){
Employee.call(this, name, age, empid, gender);
this.canbecomeDad = function(){
return true;
}
}
Avatar.prototype = Object.create(Employee.prototype);
Avatar.constructor = Avatar;
var senthil = new Avatar("Senth", '40', "A123", "M" );
console.log(senthil.empId);
console.log(senthil.gender);
console.log(senthil.name);
console.log(senthil.canbecomeDad());
console.log(senthil.age);
console.log(senthil.goestoWork());
console.log(senthil.finishesProject());
</script></body>
</html>
@eshrinivasan
Copy link
Author

eshrinivasan commented Dec 11, 2020

Inheritance example with "Class", "extends", "constructor", "super"

class Vehicle {
 
  constructor (name, type) {
    this.name = name;
    this.type = type;
  }
 
  getName () {
    return this.name;
  }
 
  getType () {
    return this.type;
  }
 
}
class Car extends Vehicle {
 
  constructor (name) {
    super(name, 'car');
  }
 
  getName () {
    return 'It is a car: ' + super.getName();
  }
 
}
let car = new Car('Tesla');
console.log(car.getName()); // It is a car: Tesla
console.log(car.getType()); // car
class Monster{
       constructor(){
             this.health = 100;
        }
       growl() {
           console.log("Grr!");
       }

}

class Monkey extends Monster {
        constructor(){
            super(); //don't forget "super"
            this.bananaCount = 5;
        }


        eatBanana() {
           this.bananaCount--;
           this.health++; //Accessing variable from parent class monster
           this.growl(); //Accessing function from parent class monster
        }

}

@eshrinivasan
Copy link
Author

eshrinivasan commented Dec 11, 2020

var promise = new Promise(function(resolve, reject) {
  // do a thing, possibly async, then…

request.get(options, function(err, resp, body) {
            if (err) {
                 reject(Error(err+"It broke"));
            } else {
                 resolve("Stuff worked!");
            }
        });
});

promise.then(function(result) {
  console.log(result); // "Stuff worked!"
}, function(err) {
  console.log(err); // Error: "It broke"
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment