JavaScript Classes review

Posted on Updated on

Some basic notes

// *****START CLASS***********************************************

//The constructor function is called automatically when an instance of the class is created.
// a special method for creating and initializing an object created with a class.
// There can only be one special method with the name “constructor” in a class.
class Person {
constructor (name,age) { //this is a function
// console.log(name,age)
this.name=name;
this.age=age;
}
//methods
getUserDescription(){return `${this.name} is ${this.age} year(s) old`}
}

// *****END CLASS***********************************************

var me = new Person(“Andrew”,25); //arguments are passed to constructor function
// console.log(“this.name”, me.name)
// console.log(“this.age”, me.age)
var description = me.getUserDescription(); // Andrew is 25 year(s) old
console.log(description);

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s