“JavaScript Getter” Kode Jawaban

JavaScript Getter

const student = {
    firstName: 'Monica',
    
    //accessor property(setter)
    set changeName(newName) {
        this.firstName = newName;
    }
};

console.log(student.firstName); // Monica

// change(set) object property using a setter
student.changeName = 'Sarah';

console.log(student.firstName); // Sarah
SAMER SAEID

JavaScript Getter

const student = {

    // data property
    firstName: 'Monica',
    
    // accessor property(getter)
    get getName() {
        return this.firstName;
    }
};

// accessing data property
console.log(student.firstName); // Monica

// accessing getter methods
console.log(student.getName); // Monica

// trying to access as a method
console.log(student.getName()); // error
SAMER SAEID

Jawaban yang mirip dengan “JavaScript Getter”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya