Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Example of Private Class Methods and Accessors in es12

// Let's create a class named User.
class User {
    constructor() {}

    // The private methods can be created by prepending '#' before
    // the method name.
    #generateAPIKey() {
        return "d8cf946093107898cb64963ab34be6b7e22662179a8ea48ca5603f8216748767";
    }

    getAPIKey() {
        // The private methods can be accessed by using '#' before
        // the method name.
        return this.#generateAPIKey();
    }
}

const user = new User();
const userAPIKey = user.getAPIKey();
console.log(userAPIKey); // This will print: d8cf946093107898cb64963ab34be6b7e22662179a8ea48ca5603f8216748767
Comment

Private Class Methods and Accessors in es12

// Let's create a class named Str.
class Str {
    // The private attributes can be created by prepending '#'
    // before the attribute name.
    #uniqueStr;

    constructor() {}

    // A private Setters can be created by prepending '#' before
    // the Setter name.
    set #generateUniqueStringByCustomLength(length = 24) {
        const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        let randomStr = "";

        for (let i = 0; i < length; i++) {
            const randomNum = Math.floor(Math.random() * characters.length);
            randomStr += characters[randomNum];
        }

        this.#uniqueStr = randomStr;
    }

    // Public Setter
    set setRandomString(length) {
        this.#generateUniqueStringByCustomLength = length;
    }

    // A private getter can be created by prepending '#' before
    // the Getter name.
    get #fetchUniqueString() {
        return this.#uniqueStr;
    }

    // Public Getter
    get getRandomString() {
        return this.#fetchUniqueString;
    }
}

const str = new Str();
// Calling a public Setter which will then access the private Setter
// within the class.
str.setRandomString = 20;

// Calling a public Getter which will then access the private Getter
// withing the class.
const uniqueStr = str.getRandomString;
console.log(uniqueStr); // This will print a random string everytime you execute the Getter after the Setter.
Comment

PREVIOUS NEXT
Code Example
Javascript :: join () method to join all elements of the array into a string to reverse an string 
Javascript :: how to customize reactnative view dropshadow 
Javascript :: Example code of using inner blocks in Wordpress with ESNext 
Javascript :: Array helper functions in ES6 
Javascript :: Classes and constructor functions in ES6 
Javascript :: currying in javascript mdn 
Javascript :: hide header in next js page 
Javascript :: format file using jq input curl 
Javascript :: unique elements 
Javascript :: lavania 
Javascript :: GitHub Personal Access Token is not set, neither programmatically, nor using env "GH_TOKEN" electronjs 
Javascript :: angular cache interceptor 
Javascript :: Learning Arrow function Syntax 
Javascript :: javascript while function is not defined wait 
Javascript :: postfix and prefix increment in javascript 
Javascript :: jquery input valueadd 
Javascript :: @click:append 
Javascript :: suffic prefix jsps 
Javascript :: Baris (Record/Tuple adalah] 
Javascript :: angular view not changing on model 
Javascript :: `ForwardRef(ListboxComponent)`, expected a ReactNode. at ListboxComponent 
Javascript :: ProgressBar from color to color 
Javascript :: kubernetes get cluster 
Javascript :: como retirar um numero de um array js 
Javascript :: filter-vs-map-reactjs-and-jsx 
Javascript :: node sass version 6.0.0 is incompatible with 4.0.0 
Javascript :: react native Stack Navigation Prop unused variable 
Javascript :: browser extensions settings page 
Javascript :: js how to display value in html binding 
Javascript :: emacs some part of the text read only 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =