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

console.log not show private members of javascript #2528

Open
Fenzland opened this issue Jun 15, 2019 · 9 comments
Open

console.log not show private members of javascript #2528

Fenzland opened this issue Jun 15, 2019 · 9 comments
Labels
cli related to cli/ dir suggestion suggestions for new features (yet to be agreed)

Comments

@Fenzland
Copy link
Contributor

test.ts

class Foo
{
	private foo= 8;
}

console.log( new Foo, );

// expect:   Foo { foo: 8 }
// actually: Foo { foo: 8 }

test.js

class Foo
{
	#foo= 8;
}

console.log( new Foo, );

// expect:   Foo { #foo: 8 }
// actually: Foo {}
@kitsonk
Copy link
Contributor

kitsonk commented Jun 15, 2019

The output with the TypeScript private is irrelevant because it isn't a runtime thing, private is a TypeScript only construct at that point.

There appears to be inconstancy between Chrome and Node.js on this.

Chrome 74:

class Foo
{
	#foo= 8;
}

console.log( new Foo, );
VM39:6 Foo {#foo: 8}
undefined

Node.js 12.4:

> class Foo
... {
... 	#foo= 8;
... }
undefined
> 
> console.log( new Foo, );
Foo {}
undefined

I would have expected private members to not be logged actually, since they would be like non-enumerable properties too. But clearly there is inconsistency.

@Fenzland
Copy link
Contributor Author

I think private members should be invisible for runtime but visible for debug. console.log is designed for debugging. Actually it is in In browser. But in Nodejs, it seams be used for write something on stdout.

We should clear what console.log be, a debugger or a stdout printer?

In my option, it should be debuger.

  1. In deno, we have Deno.stdout.write for stdout but not have debugger for debug.
  2. console.log is designed for that at the first.

@ry
Copy link
Member

ry commented Jun 17, 2019

How does rust handle this?

println!("object {:?}", some_object);

What about in Go

fmt.Printf("%+v\n", someObject)

Do they display private members?

@kitsonk
Copy link
Contributor

kitsonk commented Jun 17, 2019

Rust debug display log them but with no indication of their visibility:

#[derive(Debug)]
pub struct Foo {
    foo: i64,
    pub bar: i64,
}

pub fn main() {
  let foo = Foo { foo: 8, bar: 8 };

  println!("{:?}", foo);
}

Outputs:

Foo { foo: 8, bar: 8 }

@kevinkassimo
Copy link
Contributor

@Fenzland
Copy link
Contributor Author

Node's stringify has been tweaked and optimized over many years, but without private properties.
To a new spec like this, node is experienceless too.

@stale
Copy link

stale bot commented Jan 6, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jan 6, 2021
@kitsonk kitsonk added cli related to cli/ dir suggestion suggestions for new features (yet to be agreed) and removed stale labels Jan 7, 2021
@kitsonk
Copy link
Contributor

kitsonk commented Jan 7, 2021

Node 15 still doesn't support this, but Chromium still does. I am not sure we can get the information from inside the isolate, as the private members shouldn't be observable, so it would require an op out to rusty_v8, which might be tricky, so this wouldn't be straight forward, but I can see the benefit.

@cjihrig
Copy link
Contributor

cjihrig commented Jun 24, 2022

Relevant discussion from Node.js, including a hacky way to display private members (that I am not suggesting we implement here): nodejs/node#27404

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli related to cli/ dir suggestion suggestions for new features (yet to be agreed)
Projects
None yet
Development

No branches or pull requests

5 participants