Extends

Joined
Dec 25, 2020
Messages
1
Reaction score
0
Hello...i have a question.
How am i supposed to use the keywrod 'extends' in this JavaScript code

Code:
let Books = {
    author:'',
    title:'',
    pages: '',
    isCheckedOut: false,
    ratings: [],
   
    getAverageRating: function () {
        let total = 0;
        for(var i in this.ratings){
            total+=this.ratings;
        }
        console.log(total/this.ratings.length);
    },
    toggleCheckOutStatus: function ()
    {
        if (this.isCheckedOut == false) 
        {
            return this.isCheckedOut = true;
        }
        else
        {
            return this.isCheckedOut;
        }
    },
    addRating: function () {
        let add = parseInt(prompt('Add rating!'));
        for (let i = 1; i <= 5; i++) {
            if (add > 5 || add < 1) 
            {
                add = parseInt(prompt('The rating must be between 1 and 5'));  
            }
            else 
            {
                this.ratings.push(add);
            } 
        }
    },
    get getAuthor()
    {
        let author1 = prompt('Enter a name!');
        return author1;
    },
    get getTitle() 
    {
        let title1 = prompt('Enter a title!');
        return title1;
    },
    get getPages() 
    {
        let pages1 = parseInt(prompt('Enter a number of pages!'));
        return pages1;
    }
}
let Movies = {
    director:'',
    title:'',
    runTime:'',
    isCheckedOut: false,
    raitings: [],
    getAverageRating: function () {
        let total = 0;
        for(var i in this.ratings){
            total+=this.ratings;
        }
        console.log(total/this.ratings.length);
    },
    toggleCheckOutStatus: function ()
    {
        if (this.isCheckedOut == false) 
        {
            return this.isCheckedOut = true;
        }
        else
        {
            return this.isCheckedOut;
        }
    },
    addRating: function () {
        let add = parseInt(prompt('Add rating!'));
        for (let i = 1; i <= 5; i++) {
            if (add > 5 || add < 1) 
            {
                add = parseInt(prompt('The rating must be between 1 and 5'));  
            }
            else 
            {
                this.ratings.push(add);
            } 
        }
    },
    get getDirector()
    {
        let director1 = prompt('Enter a name!');
        return director1;
    },
    get getTitle() 
    {
        let title1 = prompt('Enter a title!');
        return title1;
    },
    get getRunTime() 
    {
        let runTime1 = parseInt(prompt('Enter a run time!'));
        return runTime1;
    }

}
 
Last edited by a moderator:
Joined
Nov 13, 2020
Messages
302
Reaction score
38
The "extends" keyword is used on a new class to inherit all the methods from another class and extend the methods.
You do not have a CLASS defined here. So can't use extends.

JS does not exist in a vacuum. You need HTML for things to work. You did not show any.

I have never seen functions in a variable declaration. The let statement declares a block-scoped local variable, optionally initializing it to a value. Same as VAR.
 
Joined
Nov 8, 2020
Messages
8
Reaction score
1
If you want to extend the property of this object to a new object you should write your code in a class and constructor method. Example:

JavaScript:
class MyObj {
  constructor(name) {
    this.name = name;
  }
}

const myObj = new MyObj('I am Jhone');

class AnotherObj extends MyObj {
  constructor(name, age) {
    super(name);
    this.age = age;
  }
  sayHi(){
        return (`Hi ${this.name} and my age is ${this.age}`)
    }
}

const another = new AnotherObj('Imran', '20');
console.log(another.sayHi())
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top