為什么“this.userid”正在分配未定義的值?“this”指向類還是物件本身?雖然timestamp_generate_function() 有效。
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"
};
uj5u.com熱心網友回復:
“this”指向類還是物件本身?
是的。 this指向物件本身。
為什么“this.userid”正在分配未定義的值?this.userid在定義
屬性 ( ) 之前(在建構式中或其他任何地方),您試圖根據它設定值。
相反,你會想要在你的建構式中做這樣的事情:
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"
};
請注意,當我們在物件本身的實體內部時,我們正在建構式中設定值。您可以這樣做,或者如果您愿意,可以使用其他方法。無論哪種方式,您都必須先實體化物件,然后才能訪問/應用您在構造程序中設定的屬性。
uj5u.com熱心網友回復:
“this”指向類還是物件本身?
在這種情況下,this指的是物件。不,不是 JavaScript 的物件 ( {}) 資料型別,而是price_update使用new price_update().
哦,對了,你有一個錯字(user_id1而不是user_id在建構式引數中)。user_id沒有在那里定義(即使 SO mods 編輯了問題),因此未定義。
原帖摘錄:
class price_update {
constructor(api_key, url_endpoint, user_id1, 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"
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/402236.html
標籤:javascript 节点.js 目的
