javaScript Class

PHOTO EMBED

Tue Aug 08 2023 18:08:56 GMT+0000 (Coordinated Universal Time)

Saved by @h_torbatian #javascript #class #oop #this

class ClassMates{
	constructor(name,age){
    	this.name=name;
      	this.age=age;
    }
  	displayInfo(){
    	return this.name + "is " + this.age + " years old!";
    }
}

let classmate = new ClassMates("Mike Will",15);
classmate.displayInfo();  // result: Mike Will is 15 years old!
content_copyCOPY

در هنگام استفاده از new محیط اجرا تغییر می‌کند و شی this دیگر به محیط اجرا جهانی اشاره نمی‌کند و استفاده از this در داخل class به خود آن class اشاره می‌کند.