簡體   English   中英

類構造函數中選項對象的默認值

[英]Default value for options object in class constructor

我創建了一個類,我想為用戶不提供任何參數的情況設置一些值的默認選項。 我最近從一個帶有多個參數的構造函數轉到了一個對象,因為我相信當用戶創建一個新的類實例時它有助於提高可讀性。

以下是我之前的做法:

module.exports = class User {
    constructor(name = "Joe", age = 47) {
        this.name = name;
        this.age = age;
    }
}

const User = require("./user");

const user = new User(); // Defaults to "Joe" and 47

實際上,我的班級有更多的參數,使用它時的可讀性變得很難。 所以為了解決這個問題,我切換到一個工作得更好的選項對象,但我似乎無法設置默認值。 當我嘗試使用this.name它會說this.name = options.name || "Joe" Cannot read property 'name' of undefined this.name = options.name || "Joe" Cannot read property 'name' of undefined即使我認為我將默認值設置為“Joe”

我現在就是這樣做的:

module.exports = class User {
    constructor(options) {
        this.name = options.name || "Joe";
        this.age = options.age || 47;
    }
}

const User = require("./user");

const user = new User();

確保對象本身有默認值。

module.exports = class User {
    constructor(options) {
        options = options || {}
        this.name = options.name || "Joe";
        this.age = options.age || 47;
    }
}

您可以為選項設置默認值,即{}

class User {
    constructor(options = {}) {
        this.name = options.name || "Joe";
        this.age = options.age || 47;
    }
}

或首先檢查options是否真實,然后訪問該值。

class User {
    constructor(options) {
        this.name = options && options.name || "Joe";
        this.age = options && options.age || 47;
    }
}

你實際上只需要一個oneliner:

const defaultUser = {
  name: "Joe",
  age: 47
};

module.exports = class User {
  constructor(options) {
     Object.assign(this, defaultUser,  options)
  }
}

如果要更改為配置模式,仍可以保留默認參數語法:

module.exports = class User {
    constructor({ name = "Joe", age = 47 } = {}) {
        this.name = name;
        this.age = age;
    }
}

const User = require("./user");

const user = new User(); // Defaults to "Joe" and 47

“類擴展價值#<object> 不是構造函數或 null"<div id="text_translate"><p> 感謝閱讀我的帖子 我的代碼出現了這個錯誤:“Class extends value # is not a constructor or null” 這是我的代碼,我正在嘗試導出/導入類。</p><p> 怪物.js:</p><pre> const miniMonster = require("./minimonster.js"); class monster { constructor(options = { name }, health) { this.options = options; this.health = 100; this.heal = () => { return (this.health += 10); }; } } let bigMonster = new monster("Godzilla"); console.log(bigMonster); console.log(bigMonster.heal()); let mini = new miniMonster("Demon"); console.log(mini); console.log(mini.heal()); module.exports = monster;</pre><p> 迷你怪物.js:</p><pre> const monster = require("./monster.js"); class miniMonster extends monster { constructor(options) { super(options); this.health = 50; this.heal = () => { return (this.health += 5); }; } } let miniM = new miniMonster("Jon"); console.log(miniM); module.exports = miniMonster;</pre><p> 感謝您提供的任何幫助,</p><p> 祝你有美好的一天</p></div></object>

[英]"Class extends value #<Object> is not a constructor or null"

類型錯誤:Class 擴展值 #<object> 不是反應js中的構造函數或null<div id="text_translate"><p> 我試圖超越一些主要反應按鈕組件的 function,但是在擴展主要反應按鈕組件 class 時出現此錯誤。 這是我的代碼:</p><pre> import { Button } from "primereact/button"; class BtnBox extends Button { constructor(props) { super(props); this.renderLabel = this.renderLabel.bind(this); } renderLabel() { return &lt;span&gt;icon&lt;/span&gt;; } } class IconButton extends React.Component { render() { return ( &lt;div className="icon-button"&gt; &lt;BtnBox {...this.props} /&gt; &lt;/div&gt; ); } } export default IconButton;</pre></div></object>

[英]TypeError: Class extends value #<Object> is not a constructor or null in react js

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 javascript類構造函數中的默認枚舉值 類構造函數angular 2的默認數組值 更改 Javascript 中的構造函數 class 中屬性的默認值 “類擴展價值#<object> 不是構造函數或 null"<div id="text_translate"><p> 感謝閱讀我的帖子 我的代碼出現了這個錯誤:“Class extends value # is not a constructor or null” 這是我的代碼,我正在嘗試導出/導入類。</p><p> 怪物.js:</p><pre> const miniMonster = require("./minimonster.js"); class monster { constructor(options = { name }, health) { this.options = options; this.health = 100; this.heal = () => { return (this.health += 10); }; } } let bigMonster = new monster("Godzilla"); console.log(bigMonster); console.log(bigMonster.heal()); let mini = new miniMonster("Demon"); console.log(mini); console.log(mini.heal()); module.exports = monster;</pre><p> 迷你怪物.js:</p><pre> const monster = require("./monster.js"); class miniMonster extends monster { constructor(options) { super(options); this.health = 50; this.heal = () => { return (this.health += 5); }; } } let miniM = new miniMonster("Jon"); console.log(miniM); module.exports = miniMonster;</pre><p> 感謝您提供的任何幫助,</p><p> 祝你有美好的一天</p></div></object> 具有默認參數值的es6類構造函數上的nodejs錯誤 對象在Mootools中的類選項中? javascript中options對象的默認值 未捕獲的類型錯誤:類擴展值 #<Object> 不是構造函數或 null 類型錯誤:Class 擴展值 #<object> 不是反應js中的構造函數或null<div id="text_translate"><p> 我試圖超越一些主要反應按鈕組件的 function,但是在擴展主要反應按鈕組件 class 時出現此錯誤。 這是我的代碼:</p><pre> import { Button } from "primereact/button"; class BtnBox extends Button { constructor(props) { super(props); this.renderLabel = this.renderLabel.bind(this); } renderLabel() { return &lt;span&gt;icon&lt;/span&gt;; } } class IconButton extends React.Component { render() { return ( &lt;div className="icon-button"&gt; &lt;BtnBox {...this.props} /&gt; &lt;/div&gt; ); } } export default IconButton;</pre></div></object> WebdriverIO自定義報告程序-TypeError:類擴展值# <Object> 不是構造函數或null
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM