Skip to main content
added 21 characters in body
Source Link

Although often we are used to seeing objects with public properties without any access control, JavaScript allows us to accurately describe properties. In fact, we can use descriptors in order to control how a property can be accessed and which logic we can apply to it. Consider the following example:

var personemployee = {
    namefirst: "John""Boris",
    surnamelast: "Smith""Sergeev",
    get fullName() {
        return this.namefirst + " " + this.surname;last;
    },
    set fullName(value) {
        var parts = value.toString().split(" ");
        this.namefirst = parts[0] || "";
        this.surnamelast = parts[1] || "";
    },
    email: "john"boris.smith@packtpubsergeev@example.com"
};

The final result:

console.log(personemployee.fullName); //JohnBoris SmithSergeev
personemployee.fullName = "Mario"Alex Rossi";Makarenko";

console.log(personemployee.namefirst);//MarioAlex
console.log(personemployee.surnamelast);//RossiMakarenko
console.log(personemployee.fullName);//MarioAlex RossiMakarenko

Although often we are used to seeing objects with public properties without any access control, JavaScript allows us to accurately describe properties. In fact, we can use descriptors in order to control how a property can be accessed and which logic we can apply to it. Consider the following example:

var person = {
    name: "John",
    surname: "Smith",
    get fullName() {
        return this.name + " " + this.surname;
    },
    set fullName(value) {
        var parts = value.toString().split(" ");
        this.name = parts[0] || "";
        this.surname = parts[1] || "";
    },
    email: "john.smith@packtpub.com"
};

The final result:

console.log(person.fullName); //John Smith
person.fullName = "Mario Rossi";

console.log(person.name);//Mario
console.log(person.surname);//Rossi
console.log(person.fullName);//Mario Rossi

Although often we are used to seeing objects with public properties without any access control, JavaScript allows us to accurately describe properties. In fact, we can use descriptors in order to control how a property can be accessed and which logic we can apply to it. Consider the following example:

var employee = {
    first: "Boris",
    last: "Sergeev",
    get fullName() {
        return this.first + " " + this.last;
    },
    set fullName(value) {
        var parts = value.toString().split(" ");
        this.first = parts[0] || "";
        this.last = parts[1] || "";
    },
    email: "boris.sergeev@example.com"
};

The final result:

console.log(employee.fullName); //Boris Sergeev
employee.fullName = "Alex Makarenko";

console.log(employee.first);//Alex
console.log(employee.last);//Makarenko
console.log(employee.fullName);//Alex Makarenko
Source Link

Although often we are used to seeing objects with public properties without any access control, JavaScript allows us to accurately describe properties. In fact, we can use descriptors in order to control how a property can be accessed and which logic we can apply to it. Consider the following example:

var person = {
    name: "John",
    surname: "Smith",
    get fullName() {
        return this.name + " " + this.surname;
    },
    set fullName(value) {
        var parts = value.toString().split(" ");
        this.name = parts[0] || "";
        this.surname = parts[1] || "";
    },
    email: "[email protected]"
};

The final result:

console.log(person.fullName); //John Smith
person.fullName = "Mario Rossi";

console.log(person.name);//Mario
console.log(person.surname);//Rossi
console.log(person.fullName);//Mario Rossi