Java script object in function use this


1. function People(name, age, hobby) {
2. this.name = name;
3. this.age = age;
4. this.hobby = hobby;
5. }
  * // menambahkan introMyself ke People
6. People.prototype.introMyself = function () {
7. // this -> People
8. setTimeout(() => {
9. // this -> People
10. console.log(`Hello! Nama saya ${this.name}, umur saya ${this.age}.`)
11. console.log(`Hobby saya adalah ${this.hobby}`)
12. }, 300)
13. }
  * const programmer = new People("John", 18, ["Coding", "Read book", "Ping-pong"]);
14. programmer.introMyself();
    * /* output:
15. Hello! Nama saya John, umur saya 18.
16. Hobby saya adalah Coding,Read book,Ping-pong
17. */

i dont  understant what line 6 meat?
is prototype is javascript keyword?

setTimeout is a function from the js toolkit, probably defined in Object.prototype (this is not too important now).

It will execute a function that you pass as an argument, after Xmiliseconds.
The function is executed only once, in opposition to setInterval, which works the same but will keep executing the function.

setTimeout(function, interval)

thank’s for explanation

1 Like