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

Define the "self" global name #650

Closed
guybedford opened this issue Aug 31, 2018 · 34 comments · Fixed by #4131
Closed

Define the "self" global name #650

guybedford opened this issue Aug 31, 2018 · 34 comments · Fixed by #4131
Assignees

Comments

@guybedford
Copy link
Contributor

The most widely available global is self as it is in both workers and in the main window in browsers.

It would be very useful to define this global for cross-platform support.

As a further point it could even be worth having it replace window since typeof window is typically done for an isBrowser detection, while the same cannot necessarily be said of self.

@ztplz
Copy link
Contributor

ztplz commented Sep 1, 2018

Why not use global, it is more clearly.

@benjamingr
Copy link
Contributor

I recommend we go with global which is stage 3 and is in the TC39 process

@kitsonk
Copy link
Contributor

kitsonk commented Sep 1, 2018

@benjamingr which is now not going forward with global. I would be all for that except that it is now not going to progress until an alternative name to global is identified.

Because of the global debacle I would tend to agree with @guybedford while we await the outcome of the stuck TC39 proposal.

@benjamingr
Copy link
Contributor

@ljharb - any strong opinions here?

@ljharb
Copy link
Contributor

ljharb commented Sep 2, 2018

self is a terrible name, and the new name is already selected. global isn’t web compatible.

For something node-like, I’d suggest either providing what node does - global - or nothing, pending the new name landing in the language (which will never be “self”).

@benjamingr
Copy link
Contributor

and the new name is already selected. global isn’t web compatible.

Well, if the new name is already selected - what is it? 😅

@ljharb
Copy link
Contributor

ljharb commented Sep 2, 2018

Since it’s still in the process of shipping to browsers, and since there’s a real risk of trolling and of it being not web compatible to do so, I’m trying to keep it under the radar, and I’d ask everyone here to do so as well.

Please contact me privately if there’s an urgency to know.

@benjamingr
Copy link
Contributor

@ljharb 😮 ok.

As far as I'm concerned there's no urgency to know. For what it's worth I think it's entirely reasonable for the TC to not put up with trolling at any amount or capacity.

I recommend we wait for the TC.

@ljharb
Copy link
Contributor

ljharb commented Sep 2, 2018

I don’t necessarily mean in a specific arena; I’m talking like people putting up websites or publishing packages that intentionally conflict with the name.

@kitsonk
Copy link
Contributor

kitsonk commented Sep 2, 2018

My opinion then is to just wait for the public TC39 announcement. I don't think there is any rush, and TC39 should still be ahead of a stable Deno, would be my guess.

@MasterJames
Copy link

'self' gets my NeXT vote.
Personally nothing is like hoisting and makes lots of sense to me as well.

@ry
Copy link
Member

ry commented Sep 4, 2018

Why don't we just use all of them? global, self, and window?

@ljharb
Copy link
Contributor

ljharb commented Sep 4, 2018

Using window or self will absolutely break code that uses the presence of those things to test for "is a browser". Neither should be set in a server environment.

@MasterJames
Copy link

All works. Let there code break, easy to fix. I think there is better ways to test for browser. Maybe testing for browser on a server is not critical either.
Still if you think how javascript not strict mode hoisted variables all you need to do is remember to initialize and in a single threaded way with scope it's not a bad solution that originally existed. Anyway I always thought self should be implied. If you are in a function, and reference a value name that's not declared it should automatically imply it's in 'self'. Is that totally junior and stupid understanding or does it not make things better to imply self?

@GrosSacASac
Copy link

According to https://github.com/tc39/proposal-global/blob/master/spec.md
It would be globalThis

@ExE-Boss
Copy link

ExE-Boss commented Dec 19, 2018

I personally prefer global, which was used by node, and the broken scripts use it for a browser/node detection.

Deno is probably closer to node than to a web browser.

See also tc39/proposal-global#36

@ljharb
Copy link
Contributor

ljharb commented Dec 19, 2018

deno could certainly use global, just like node; but it'd have to also provide globalThis to be a compliant JavaScript engine.

@kt3k
Copy link
Member

kt3k commented Dec 26, 2018

deno seems already having globalThis object (probably because of V8 update)

$ deno -v
deno: 0.2.3
v8: 7.1.302.4
typescript: 3.2.1
MacBook-Pro-3:~ kt3k$ deno
> Object.keys(globalThis)
[ "libdeno", "denoMain", "window", "atob", "btoa", "fetch", "clearTimeout", "clearInterval", "setTimeout", "setInterval", "Blob", "File", "URLSearchParams", "Headers", "FormData", "TextEncoder", "TextDecoder", "deno" ]

ref: https://v8.dev/blog/v8-release-71

@bartlomieju
Copy link
Member

@ry is this still relevant with our current approach?

@kitsonk
Copy link
Contributor

kitsonk commented Feb 12, 2019

We have globalThis as part of the type definitions now, I say we close this.

@ry ry closed this as completed Feb 12, 2019
@josephrocca
Copy link
Contributor

@ry @kitsonk If we don't have self et al, then doesn't this mean that a bunch of code will need to be re-written to be compatible with Deno? I'm having to re-write all my Worker scripts right now which is kinda annoying. Not a huge deal, but I thought Deno was trying to be fairly compatible with the browser and node so that we can bootstrap off the existing ecosystem of js packages?

@MasterJames
Copy link

One can just patch like I make an 'und' that is 'undefined'... no? this = self or what have you. Anyway I would think it is not impossible to cover the bases internally by default and have all things desired or needed without a painful impact on performance or memory, or is that wrong in this case?

@ry
Copy link
Member

ry commented Jun 2, 2019

Sorry I closed this too soon. I do want to have “self” in workers.

@ry ry reopened this Jun 2, 2019
@kitsonk
Copy link
Contributor

kitsonk commented Jun 2, 2019

Yeah, sorry... The original post was suggesting self as the global for every runtime. globalThis and window for main thread runtimes, and globalThis and self for workers make sense.

Another somewhat tangental consideration is that we only have one type library. Ideally we would have 3 type libraries for Deno. One common, and then one small main runtime and one small worker library. We would also need to instruct the compiler if it is main or worker code to maximise the type safety.

I would suggest we tackle all this under a different issue though. I originally opened #1866 to deal with that, though it isn't explicit enough.

@ExE-Boss
Copy link

ExE-Boss commented Jun 2, 2019

I still hate that the web global object is window, but this is sadly the reality we live in.

Deno could however decide to just use globalThis, which would refer to a generic global scope that only has the ECMAScript globals as its properties.

@ljharb
Copy link
Contributor

ljharb commented Jun 3, 2019

globalThis is the global object - Function(‘foo = true’)() must create a foo property on it, so it can’t be restricted to just the language globals - it must include all globals.

For consistency with the web, any usage of windows or selfmust thus be=== globalThis`.

@ExE-Boss
Copy link

ExE-Boss commented Jun 3, 2019

I meant only have the ECMAScript properties on it by default.

@craigfay
Copy link

craigfay commented Jun 3, 2019

@kitsonk I don’t disagree, but how did you come to the conclusion about dividing the type library the way you described? If #1866 is a better place for that discussion, I’d be curious to see your answer there.

@AviVahl
Copy link

AviVahl commented Jul 16, 2019

might be worth mentioning that:

declare global {
    function startsWithPound(text: string): boolean;
}
globalThis.startsWithPound = text => text.startsWith('#')
globalThis.startsWithPound('some text');

// above passes type checks and gets proper inference for function's argument.

@morille
Copy link

morille commented Sep 25, 2019

Is the support of "this" an option for Deno ?
Many libraries are using the global "this" keyword to get access to the global context.
I just migrated one of my projects to Deno and everything works well excepted this.

@ljharb
Copy link
Contributor

ljharb commented Sep 25, 2019

@morille ES Modules must have their this set to undefined; it's only an option in sloppy mode Script. globalThis is the standard mechanism.

@morille
Copy link

morille commented Sep 25, 2019

@ljharb I understand that but it does not affect the main context AFAIK.

@ljharb
Copy link
Contributor

ljharb commented Sep 25, 2019

It affects any place where import works.

@kitsonk
Copy link
Contributor

kitsonk commented Sep 25, 2019

We should close this IMO @ry. We have globalThis and window is defined as Window & globalThis now just like out of the box TypeScript. I am not sure what else we would want to do.

johnsonjo4531 added a commit to johnsonjo4531/greenlet that referenced this issue Dec 28, 2019
[deno](https://deno.land)
[doesn't support self](denoland/deno#650)
so globalThis should get it to work there but, it's not
supported in edge and IE. So, inorder to shim we can use
the polyfill from [mozilla docs on globalThis](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis)
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

Successfully merging a pull request may close this issue.