Skip to content

Instantly share code, notes, and snippets.

@davidsharp
Created May 16, 2019 10:11
Show Gist options
  • Select an option

  • Save davidsharp/9184a2e501f5e6704717031bb4951312 to your computer and use it in GitHub Desktop.

Select an option

Save davidsharp/9184a2e501f5e6704717031bb4951312 to your computer and use it in GitHub Desktop.
An ill-advised console.log proxy allowing logging via `console.log = 'someValue'` (which actually gains some nice features)
console=new Proxy(console,{set:(obj,prop,value)=>obj[prop](value)})
/*
Use like:
console.log = 'hello world'
logs -> 'hello world'
Unexpected bonus side-effect:
const foobar = console.log = 'hello world'
logs -> 'hello world'
then!:
console.log = foobar
logs -> 'hello world'
*/
@davidsharp

Copy link
Copy Markdown
Author

Naturally, you can also set the proxy to be called something else, rather than setting it to console and then use it like myProxiedConsole.log = 'console is left untouched'

@davidsharp

Copy link
Copy Markdown
Author

Also worth pointing out, console.log(foobar) would still work as expected, because the setter never actually sets anything, so console.log is always console.log

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