there is a function:

function buildUser(first, last) { let fullName = first + " " + last; return {first, last, fullName}; } 

I create variables with it:

 let {first, last, fullName} = buildUser("User1", "LastName"); console.log(first); console.log(last); console.log(fullName); 

everything works, but why I can’t create one more variables I don’t understand. I do this:

 let {first, last, fullName} = buildUser("test1", "test1"); console.log(first); console.log(last); console.log(fullName); 

I get a syntax error:

 SyntaxError: redeclaration of let first let {first, last, fullName} = buildUser("test1", "test1"); 

if I do with new variables, they turn out to be undefined:

 let {first2, last2, fullName2} = buildUser("test1", "test1"); console.log(first2); console.log(last2); console.log(fullName2); 

what am I missing, please explain?

completely confused: such code for some reason does not work either:

 function buildUser2 (first, last, postCount) { let fullNameMy = first + " " + last; const ACTIVE_POST_COUNT = 10; return { firstMy, lastMy, fullNameMy, isActive(){ return postCount >= ACTIVE_POST_COUNT; } } } let {firstMy, lastMy, fullNameMy} = buildUser2("test2", "test2"); console.log(firstMy); console.log(lastMy); console.log(fullNameMy); 

mistake:

 ReferenceError: can't access lexical declaration `firstMy' before initialization 
  • one
    The answer and comments below answer your question well. And with an added question, you return firstMy that is not declared in the method - Vasily Barbashev

1 answer 1

Problem in the let statement

Re-declaring the same variable in the same block or function will throw a SyntaxError exception.

if I do with new variables, they turn out to be undefined:

 let {first2, last2, fullName2} = buildUser("test1", "test1"); console.log(first2); console.log(last2); console.log(fullName2); 

When restructuring assignment important field names.

An object is returned in the buildUser function buildUser

 return {first, last, fullName}; 

this is an abbreviated form of declaring object properties equivalent

 { "first": first, "last": last, "fullName": fullName, } 

In the assignment expected: first2, last2, fullName2 in the returned object they are not. so they accept the default value: undefined

Problems with the names of the properties of objects can be avoided by returning from the function not an object but an array

 function buildUser(first, last) { let fullName = first + " " + last; return [first, last, fullName]; } let [first, last, fullName] = buildUser("User1", "LastName"); console.log(first,last,fullName); let [first2, last2, fullName2] = buildUser("test1", "test1"); console.log(first2, last2, fullName2); 

Update:

mistake:

 ReferenceError: can't access lexical declaration `firstMy' before initialization 

In ECMAScript 2015, variables declared by the let statement are transferred to the top of the block. But if you refer to a variable in a block before it is declared by the let operator, it will throw a ReferenceError exception, because the variable is in the "time dead zone" from the beginning of the block to the time it was declared.

  • His second question still needs to be answered) - Vasily Barbashev
  • Well, yes, everything is correct, he also takes the names first2, last2, fullName2 , which are not in the returned object - Vasily Barbashev
  • It turns out in this version, you can only use a function once? - spectre_it
  • @ stas0k Depending on what you want to do. Write to the array for example. Either return the masses, as Grundy added to the answer - Vasily Barbashev
  • @ Vasily Barbashev I wanted to use it several times like this: let {first, last, fullName} = buildUser ("User1", "LastName"); let {first, last, fullName} = buildUser ("User2", "LastName"); - spectre_it