繁体   English   中英

JavaScript:为 object 设置默认值

[英]JavaScript:Set default values ​for an object

您好,我正在尝试找到一种方法来在单独的文件中设置定义的值以填充我的 function:

我的 function index.js:

const Modes = (array) => {
  return {
    name: array.name,
    funcionarioIncrease: array.funcionarioValor,
    socioIncrease: array.socioValue,
    faturamento: array.faturamento
  }
}

我的架构.json

    {   
  "name": "SIMPLES NACIONAL – MEI",   "funcionarioIncrease": 49.99,  
  "socioIncrease": 0,
  "FATURAMENTO": [
        {
          "name": "ATÉ 30.000,00",
          "value": 149.99
        },
        {
          "name": "De 30.001,00 a 50.000,00 ",
          "value": 199.90
        }   ] 
}

但我不知道该怎么做,如果.json 是正确的方法

您可以使用一个充满常量值的 JS 文件并导入它。

例如:

costants.js

export const NAME="MyTestName";
export const SOCIO_INCREASE=0;
export const FATURAMENTO=[{id:1}]

在你的功能组件中:

函数式.js

import {NAME,SOCIO_INCREASE,FATURAMENTO} from "./constants.js"

const Modes = ({
    name=NAME,
    socioIncrease=SOCIO_INCREASE,
    faturamento=FATURAMENTO
}) => {
  return {
    name,
    socioIncrease,
    faturamento
  }
}

请记住,如果您将其中一些道具传递给功能组件,则默认值将被覆盖。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM