Article From:https://segmentfault.com/q/1010000012140239
Question:
 <script type="text/javascript">
    function demo(name = "Zhang three ", age = 30) {/ / here can be used () or function demo ({name = "Zhang three", age = 30}).Console.Log ("Name:" + name);Console.log ("nianl:" + age);}Demo ();< /script>

If the code is annotated, adding brackets and not adding them is fine. Just after adding brackets, you also need to add {}. So, what is the difference between () and {}?

Answer 0:

Simple understanding: Deconstruction assignment means the corresponding position, corresponding.key Automatic assignment.

Use() still{},Depends on the parameters of the functions you define. Let’s take a look at the following example.


The first is: when the parameter of an function is an object, it needs to be used.{}。So it can pass the same parameters in the function.key Getting to the parameter objectkey The corresponding value.

function demo({name, age}) {
  console.log(`Name: ${name}`)Console.log (` age: ${age}` ')}Demo ({Name: 'Zhang three',Age: 24})

The second is: when the parameters in the function are multiple, use this time.()In fact, this is not a structural assignment, it’s really justES5 Only the parameters in your code are used.ES6 Default settings for the parameters in the system

function demo(name = "Zhang three ", age = 30) {Console.log ("Name:" + name);Console.log ("nianl:" + age);}Demo ();

Answer 1:

{} is the literal quantity of an object without adding the two parameter of a function. In other words, the difference between the number and type of function parameters.

Answer 2:

But it can’t be.

Answer 3:

Parentheses call demo ({}) so called; deconstruction assign your structure to be consistent.

Answer 4:

The difference is parameter () order and {} disorder. {} must pass an empty object.

Answer 5:
function demo(name = "Zhang three, age = 30) / / this is the two parameter that is passed in, and then set the default value.Function demo ({name = "Chang three", age = 30}) / / this is the introduction of an object and then the deconstruction of the object.

Answer 6:
function demo({name, age} = {name: "Zhang three ", age: 30}) {Console.log ("Name:" + name);Console.log ("nianl:" + age);}Demo ();

This is acceptable. The equal sign should be the meaning of the default value.

Answer 7:

{}For incoming objects, objects can contain multiple attributes or methods.

Leave a Reply

Your email address will not be published. Required fields are marked *