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 :: Mapping page number to index 
Javascript :: Pass Props to a Component Using Short circuit evaluation in react 
Javascript :: Allowed Blocks in Nested Blocks Component Wordpress 
Javascript :: Default function arguments in ES6 
Javascript :: javascript on enter keyup select button 
Javascript :: JS get dropdown setting 
Javascript :: where is the waypoint json file lunar client mac 
Javascript :: pass js variable to css animation 
Javascript :: convert java object to json 
Javascript :: react-navigation: Detect when screen, tabbar is activated / appear / focus / blur 
Javascript :: nestjs cors dotnot woriking 
Javascript :: react native helper packages 
Javascript :: Show / Hide Div On Radio Button Click angular 
Javascript :: js plugin for drop down with images 
Javascript :: refactor from data object 
Javascript :: discord js kick command 
Javascript :: why in the hell does JavaScript - Date getMonth() return 11 
Javascript :: alpinejs checknox selectAll 
Javascript :: change frame rate javascript 
Javascript :: Fix Blurry Canvas on Mobile Phones 
Javascript :: makestyle server side rendering 
Javascript :: como fazer um array em javascript stackoverflow 
Javascript :: ES5 Assigning Variables to Object Properties 
Javascript :: solana solana-Web3.js change for devnet lamports to production transaction 
Javascript :: how to read textbox values from html and insert them into tables using java script 
Javascript :: url(image loacation) give a class 
Javascript :: unload js object 
Javascript :: Script test to be oneOf 
Javascript :: node and bash together 
Javascript :: load json object from file frontend javascript 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =