ජාවාස්ක්රිප්ට් හි OOP හි සිතියම් ගුණාංග සැකසුම්කරුවන්
සිතියම් ගුණාංග ලබාගන්නා උපකරණ වලට අමතරව ඒවායේ සැකසුම්කරුවන් ද සෑදිය හැකිය. මෙම අවස්ථාවේදී, සැකසුම්කරු තුළ අපි දත්ත ලබා ගත යුතුය, ඒවා වෙන් කර අනුරූප කොටස් ලියා තැබිය යුතුය අවශ්ය පොදු ගුණාංගවලට.
අපි උත්සාහ කරමු. අපි ක්රියාත්මක කරමු
සිතියම් ගුණාංගයේ සැකසුම්කරු full:
class User {
constructor(name, surn) {
this.name = name;
this.surn = surn;
}
get full() {
return this.name + ' ' + this.surn;
}
set full(full) {
let [name, surn] = full.split(' ');
this.name = name;
this.surn = surn;
}
}
අපි විනාශ කිරීම සරල කරමු:
class User {
constructor(name, surn) {
this.name = name;
this.surn = surn;
}
get full() {
return this.name + ' ' + this.surn;
}
set full(full) {
[this.name, this.surn] = full.split(' ');
}
}
වර්ගයේ වස්තුවක් සාදන්න:
let user = new User('john', 'smit');
අපගේ සිතියම් ගුණාංගයට දත්ත ලියන්න සැකසුම්කරු:
user.full = 'eric jons';
වස්තුවේ ගුණාංග බලමු වෙනස් විය:
console.log(user.name); // 'eric'
console.log(user.surn); // 'jons'
console.log(user.full); // 'eric jons'