Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't define an undefined variable because it was already declared? 🤔 #3227

Closed
Chuckytuh opened this issue Feb 16, 2021 · 4 comments
Closed

Comments

@Chuckytuh
Copy link

  • Node.js Version: v12.19.0; v14.7.0
  • OS:
  • Scope (install, code, runtime, meta, other?): runtime
  • Module (and version) (if relevant): n/a

I'm a bit confused by the following snippet. That problem (?) occurs on nodejs 12 as well, but, the same snippet does not result on the same error on Chrome, for example. Can someone please explain to me why and if this isn't a bug?

node repl:

Welcome to Node.js v14.7.0.
Type ".help" for more information.
> let x = Object.create()
Uncaught TypeError: Object prototype may only be an Object or null: undefined
    at Function.create (<anonymous>)
> x
Uncaught ReferenceError: x is not defined
> let x = Object.create({})
Uncaught SyntaxError: Identifier 'x' has already been declared
>
@Lancear
Copy link

Lancear commented Feb 20, 2021

Can confirm the same thing happening with node v14 lts. i guess its because of the error at the first assignment. but its interesting behaviour for sure.

@Shigetorum635
Copy link

I will try replicating the behaviour.

@Shigetorum635
Copy link

image

NodeJS cant set variables if they error. They are kept as undefined. I suggest using

let x = {} to make objects instead of Object.create if you dont know how it works.

Object.create makes an object with an existing object. Aka:

let obj = {
a:true
}

let b = Object.create(obj)
b.a = true

A bit like typescript interfaces of some sort.

@devsnek
Copy link
Member

devsnek commented Mar 4, 2021

You can find more info in nodejs/node#8309

@devsnek devsnek closed this as completed Mar 4, 2021
@devsnek devsnek reopened this Mar 4, 2021
@devsnek devsnek closed this as completed Mar 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants