JavaScriptda OOBda ob'ektlarni solishtirish
Keling, ob'ektlarni o'z ichiga olgan o'zgaruvchilar qanday solishtirilishiga qaraylik. Ikkita o'zgaruvchi, agar ular bir xil ob'ektga havola o'z ichiga olsa, teng hisoblanadi. Keling, amalda ko'ramiz. Bizda quyidagi sinf mavjud bo'lsin:
class User {
constructor(name) {
this.name = name;
}
}
Keling, ushbu sinfning ikkita ob'ektini yaratamiz:
let user1 = new User('1');
let user2 = new User('2');
Keling, bir xil ob'ektga havola o'z ichiga olgan o'zgaruvchilarni solishtiramiz:
console.log(user1 === user1); // true
Endi esa, turli ob'ektlarga havola o'z ichiga olgan o'zgaruvchilarni solishtiramiz:
console.log(user1 === user2); // false
Quyidagi kodda solishtirish natijasi qanday bo'lishini ayting:
class Employee {
constructor(name) {
this.name = name;
}
}
let emp1 = new Employee('john');
let emp2 = new Employee('eric');
console.log(emp1 === emp2);
Quyidagi kodda solishtirish natijasi qanday bo'lishini ayting:
class Employee {
constructor(name) {
this.name = name;
}
}
let emp1 = new Employee('john');
let emp2 = new Employee('eric');
console.log(emp1 === emp1);
Quyidagi kodda solishtirish natijasi qanday bo'lishini ayting:
class Employee {
constructor(name) {
this.name = name;
}
}
let emp1 = new Employee('john');
let emp2 = new Employee('john');
console.log(emp1 === emp2);
Quyidagi kodda solishtirish natijasi qanday bo'lishini ayting:
class Employee {
constructor(name) {
this.name = name;
}
}
let emp1 = new Employee('john');
let emp2 = new Employee('eric');
console.log(emp1 !== emp1);
Quyidagi kodda solishtirish natijasi qanday bo'lishini ayting:
class Employee {
constructor(name) {
this.name = name;
}
}
let emp1 = new Employee('john');
let emp2 = emp1;
console.log(emp1 === emp2);
Quyidagi kodda solishtirish natijasi qanday bo'lishini ayting:
class Employee {
constructor(name) {
this.name = name;
}
}
let emp1 = new Employee('john');
let emp2 = new Employee('eric');
console.log(emp1 !== emp2);
Quyidagi kodda solishtirish natijasi qanday bo'lishini ayting:
class Employee {
constructor(name) {
this.name = name;
}
}
let emp1 = new Employee('john');
let emp2 = emp1;
emp2.name = 'eric';
console.log(emp1 === emp2);