xxxxxxxxxx
// constructor function
function Person () {
this.name = 'John',
this.age = 23
}
// creating objects
const person1 = new Person();
const person2 = new Person();
// adding property to constructor function
Person.prototype.gender = 'male';
// prototype value of Person
console.log(Person.prototype);
// inheriting the property from prototype
console.log(person1.gender);
console.log(person2.gender);
xxxxxxxxxx
function Person(first, last, age, gender, interests) {
this.name = {
first,
last
};
this.age = age;
this.gender = gender;
this.interests = interests;
};
function Teacher(first, last, age, gender, interests, subject) {
Person.call(this, first, last, age, gender, interests);
this.subject = subject;
}
xxxxxxxxxx
class Car {
constructor(brand) {
this.carname = brand;
}
present() {
return 'I have a ' + this.carname;
}
}
class Model extends Car {
constructor(brand, mod) {
super(brand);
this.model = mod;
}
show() {
return this.present() + ', it is a ' + this.model;
}
}
let myCar = new Model("Ford", "Mustang");
document.getElementById("demo").innerHTML = myCar.show();
xxxxxxxxxx
class Animal{
constructor()
{
this.walk = "walk";
}
}
class Dog extends Animal{
constructor()
{
super();
this.bark = "bark";
}
}
const dog = new Dog();
console.log(dog.walk);
/*must include BOTH extends and super()*/
xxxxxxxxxx
// Creating a parent object
var parent = {
name: "John",
sayHello: function() {
console.log("Hello, my name is " + this.name);
}
};
// Creating a child object
var child = Object.create(parent);
child.name = "Jane";
// Accessing properties and methods
console.log(child.name); // Output: Jane
child.sayHello(); // Output: Hello, my name is Jane
xxxxxxxxxx
class teamMember {
name;
designation = "Support web dev";
address;
constructor(name, address) {
this.name = name;
this.address = address;
}
}
class Support extends teamMember {
startSession() {
console.log(this.name, "start a support sessin");
}
}
class StudentCare extends teamMember {
buildARoutine() {
console.log(this.name, "build a routine");
}
}
const max = new Support("Max", "USA");
console.log(max);
const sarah = new StudentCare("Sarah", "UK");
console.log(sarah);
xxxxxxxxxx
class Square{
constructor(l) {
this.length = l;
}
getArea = () => {
return this.length * this.length;
}
getPerimeter = () => {
return 4 * this.length;
}
}
class Rectangle extends Square{
constructor(l, w) {
super(l);
this.width = w;
}
getArea = () => {
return this.length * this.width;
}
getPerimeter = () => {
return 2 * (this.length + this.width);
}
}
let r = new Rectangle(4, 3);
console.log("Area= ", r.getArea());
console.log("Perimeter= ", r.getPerimeter());
xxxxxxxxxx
function Teacher(first, last, age, gender, interests, subject) {
Person.call(this, first, last, age, gender, interests);
this.subject = subject;
}