Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

javascript - `arguments`无法获取`Destructuring`参数的默认值?(`arguments` Cannot get the default value of the `Destructuring` parameter?)

 const supersearch = function({a = 1, b}) { console.log(arguments[0]); console.log(a, b); }; supersearch({b: 2}); 

expected outcome:

(预期结果:)

{
  "a": 1,
  "b": 2
}

1 2

but actual results:

(但实际结果:)

{
  "b": 2
}

1 2

How should i get arguments ?

(我应该如何arguments ?)

doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

(doc: https//developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/arguments)


update:

(更新:)

A way I can think of now, is there a better way to write it?

(我现在可以想到的一种方式,有没有更好的书写方式?)

 const supersearch = function({a, b}) { console.log({...{a: 1}, ...arguments[0]}); console.log(a, b); }; supersearch({b: 2}); 

  ask by ebyte ebyte translate from so


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

hi you define one one argument of object type so that is happning

(您好,您定义了一个对象类型的一个参数,所以发生了)

 const supersearch = function({}) { alert(JSON.stringify(arguments[0])); alert(arguments[0].a + '' + arguments[0].b); }; supersearch({a:1 ,b: 2}); 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...