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
442 views
in Technique[技术] by (71.8m points)

assigning the value of function to a variable of same name in javascript

I was reading about hoisting, and in my editor I was trying this

function loginUser() { return "Hi"}

const loginUser = loginUser();

I am getting the error cannot access "loginUser" before reinitialisation. Has it got something to do with hoisting. could I get the explanation as to why I am getting this error?


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

1 Answer

0 votes
by (71.8m points)

I can't reproduce the error you are getting. My JS engines log:

SyntaxError: Identifier 'loginUser' has already been declared

Your function declaration declares a variable named loginUser.

Your const declaration declares a variable name loginUser.

const can't redeclare a variable that already exists in the same scope, hence the error.


Your error message, which seems to be an oddity of your environment, is saying that since you have just said const loginUser you can't read the value loginUser to assign to it because the new one doesn't exist yet.


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

...