Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why 'this.userid' is assigning undefined value? does "this" pointing to the class or the object itself? timestamp_generate_function() works though

Why ‘this.userid’ is assigning undefined value? does "this" pointing to the class or the object itself? timestamp_generate_function() works though.

class price_update {


    constructor(api_key, url_endpoint, user_id, store_name) {
        this.api_key = api_key;
        this.url_endpoint = url_endpoint;
        this.userid = user_id;
        this.store_name = store_name;
    }

    #updatePriceQuantity_parameters = {
        Action: "UpdatePriceQuantity",
        Format: "json",
        Timestamp: this.#timestamp_generate_function(),
        UserID: this.userid,
        Version: "1.0"
    };

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

does "this" pointing to the class or the object itself?
Yes. this points to the object itself.

Why ‘this.userid’ is assigning undefined value?
You are attempting to set the value based on a property (this.userid) before you’ve ever defined it (either in the constructor or anywhere else).

Instead, you will want to do something like this in your constructor:

class price_update {


    constructor(api_key, url_endpoint, user_id, store_name) {
        this.api_key = api_key;
        this.url_endpoint = url_endpoint;
        this.userid = user_id;
        this.store_name = store_name;
        this.#updatePriceQuantity_parameters.UserID = user_id;
    }

    #updatePriceQuantity_parameters = {
        Action: "UpdatePriceQuantity",
        Format: "json",
        Timestamp: this.#timestamp_generate_function(),
        UserID: '',
        Version: "1.0"
    };

Note that we’re setting the value in the constructor while we’re inside of the instance of the object itself. You can do it like this, or in another method if you like. Either way, you have to instantiate the object first before you and access/apply properties that you have set during construction.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading