我使用带有babel编译器的ES6功能.我有一个函数,它将选项对象作为参数:
function myFunction({ option1 = true, option2 = 'whatever' }) {
console.log(option1, option2);
// do something...
}
Run Code Online (Sandbox Code Playgroud)
当我打电话给它时,解构就会发生,一切运转良好.我想在大多数时候用默认选项调用它,所以我这样做:
myFunction({}); // true 'whatever'
Run Code Online (Sandbox Code Playgroud)
但它看起来有点奇怪.只要打电话就会更清洁:
myFunction(); // TypeError: Cannot read property 'option1' of undefined
Run Code Online (Sandbox Code Playgroud)
可能吗?
我想编写一个函数,该函数接受一个对象参数并捕获变量中所有剩余的参数。目的是允许函数接收命名参数(与位置参数相对),其中一些参数是可选的,并在函数中设置默认值。因此,用伪代码是这样的:
interface IMyProps {
a: string
b?: number
}
const myfun1 = (p: {a: string, b?:number, ...rest}) => {
const {a, b = 'hello'} = p;
}
Run Code Online (Sandbox Code Playgroud)
在Typescript 2.0中实现此目标的最佳方法是什么?
这是代码,
export function createConnect({
connectHOC = connectAdvanced,
mapStateToPropsFactories = defaultMapStateToPropsFactories,
mapDispatchToPropsFactories = defaultMapDispatchToPropsFactories,
mergePropsFactories = defaultMergePropsFactories,
selectorFactory = defaultSelectorFactory
} = {}) {...}
Run Code Online (Sandbox Code Playgroud)
什么{connectHOC = connectAdvanced ...} = {}意味着在函数参数声明中?
我知道
= {}
Run Code Online (Sandbox Code Playgroud)
可能意味着函数参数的默认值,但是前面括号内代码的用法是什么?
我希望在嵌套对象中有一个带有默认参数的函数,并且我希望能够调用它f()或仅指定单个参数。
// A function with nested objects with default parameters:
function f({ a = 1, callback = ({ name, param } = { name: "qwe", param: 123 }) } = {}) {
console.log("a:", a);
console.log("callback:", callback);
}
// And I want to run it like this:
f();
f({ callback: { params: "456" } });
// But 'callback.name' becomes undefined.Run Code Online (Sandbox Code Playgroud)
我目前正在使用对象解构模式和ES6对象解构默认参数的答案中描述的默认参数。
(function test({a = "foo", b = "bar"} = {}) {
console.log(a + " " + b);
})();
Run Code Online (Sandbox Code Playgroud)
我希望能够访问对象参数,而无需将其分配给函数体中的变量并显式列出每个键。
(function test({a = "foo", b = "bar"} = {}) {
console.log(a + " " + b);
})();
Run Code Online (Sandbox Code Playgroud)
我尝试命名对象参数,但该函数失去了将丢失的键解析为其默认值的能力。
(function test({a = "foo", b = "bar"} = {}) {
const options = {a, b};
console.log(options);
})();Run Code Online (Sandbox Code Playgroud)
在解构为命名参数时,它似乎忽略了默认参数。
这是 ES6 规范的一部分吗?有没有办法在函数体中无需额外代码即可实现所需的行为?
编辑:我删除了一个没有为问题添加上下文的多余示例。
javascript arguments destructuring default-arguments ecmascript-6
在解构对象时,我有时会遇到不知道密钥是否存在的问题,然后尝试从它们中提取值.这显然是错误,因为它们是未定义的.例如:
期待这样的事情:
{ user: { name: { first: 'Trey', last: 'Hakanson' } } }
Run Code Online (Sandbox Code Playgroud)
但我实际上得到了这个:
{ user: {} }
Run Code Online (Sandbox Code Playgroud)
并尝试像这样的错误进行解构:
const { user: { name: { first: firstName, last: lastName } } } = data
Run Code Online (Sandbox Code Playgroud)
有没有办法在解构前面分配一个默认值?比如name = { first: 'Hello', last: 'World' }如果name密钥不存在则分配?
嗨,我在这里通过函数参数Object Destructuring Demo经历了对象解构的使用示例
function drawES6Chart({size = 'big', cords = { x: 0, y: 0 }, radius = 25} = **{}**) {
console.log(size, cords, radius);
// do some chart drawing
}
// In Firefox, default values for destructuring assignments are not yet
implemented (as described below).
// The workaround is to write the parameters in the following way:
// ({size: size = 'big', cords: cords = { x: 0, y: 0 }, radius: radius =
25} = **{}**)
drawES6Chart({ …Run Code Online (Sandbox Code Playgroud) 我正在使用 React 和 Next.JS 创建一个个人项目。我试图传递一个带有默认值的参数,以便不需要每次使用此函数时都指定一个值。我怎样才能做到这一点?为了理解我希望 shouldShuffle 参数默认为 false。
const getItems = async ({shouldShuffle}) => {
// my code
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我正在尝试为选项参数定义具有以下行为的构造函数:
class Test {
constructor({
someOption = 'foo',
otherOption = 'bar',
aSeedOfSomeSort = Math.random(),
// ...
}) {
console.log(
someOption, otherOption, aSeedOfSomeSort
);
}
}
new Test({someOption: 'lol'});
new Test({otherOption: 'derp'});
new Test({aSeedOfSomeSort: 0.5});
new Test({});
new Test(); // here's the problemRun Code Online (Sandbox Code Playgroud)
这很好用,但是,我希望构造函数能够工作,以便使用所有默认参数,我不必传递空对象。我不关心对象本身是否在参数中命名,但在构造函数的上下文中,我想要一种干净的方法来直接访问所有选项,而无需命名空间或使用with.
这可能吗?
在解构函数参数时,如果函数参数未定义,该如何处理?
const product = {
label: 'Notebook',
price: 50
};
const destructSample = ({label, price}) => {
console.log(label, price);
}
destructSample(product);
destructSample(undefined);
Run Code Online (Sandbox Code Playgroud)
destructSample(undefined); 引发以下错误
const destructSample = ({label, price}) => {
^
TypeError: Cannot destructure property `label` of 'undefined' or 'null'.
at destructSample (E:\PlayGround\NodeJs\nodeCourse\playground\destructuringError.js:6:24)
at Object.<anonymous> (E:\PlayGround\NodeJs\nodeCourse\playground\destructuringError.js:11:1)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Run Code Online (Sandbox Code Playgroud)
如何解决呢?谢谢。
javascript ×8
ecmascript-6 ×7
object ×2
arguments ×1
constructor ×1
next.js ×1
reactjs ×1
typescript ×1