Skip to content

global becomes undefined #13027

Description

@macsmac

When I run this code:

Object.keys(global).forEach(k => Object.defineProperty(global, k, {set:console.log}));

global object becomes undefined

typeof global // "undefined"

But

typeof this // "object"

And if I will run Object.keys or Object.defineProperty global does not become undefined. Bug?

Activity

  1. TimothyGu commented on May 14, 2017

    @TimothyGu
    Member

    No it is not a bug. global mirrors global.global, and when you define global.global to a setter-only property, global cannot be read anymore, and typeof naturally returns 'undefined'.

  2. macsmac commented on May 14, 2017

    @macsmac
    Author

    Forget to say: defineProperty throws error "defineProperty called on non-object". We will log key.

    Object.keys(global).forEach((k) =>{ console.log(k); Object.defineProperty(global, k, {set:console.log}); });

    It logs

    global
    process
    <error>
    
  3. added
    questionIssues asking questions about Node.js.
    on May 14, 2017
  4. macsmac commented on May 14, 2017

    @macsmac
    Author

    Actually got it, because first key is global we defineProperty on it without getter and it becomes undefined and next key cannot be defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionIssues asking questions about Node.js.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions