Do Javascript private fields or methods using #
work in iOS?
For example, I have this class:
Class A {
#privateFunction() {return ""}
}
Creating an instance of class A works in Android and desktop. But in iOS, I get the following error: cannot find variable: A
I know the error is related to iOS, because if I made the privateFunction
public, it works.
I'm testing the code on a real iPhone device updated to the latest iOS version.
Update: I updated iOS to 14.5.1 but nothing changed.
Do Javascript private fields or methods using #
work in iOS?
For example, I have this class:
Class A {
#privateFunction() {return ""}
}
Creating an instance of class A works in Android and desktop. But in iOS, I get the following error: cannot find variable: A
I know the error is related to iOS, because if I made the privateFunction
public, it works.
I'm testing the code on a real iPhone device updated to the latest iOS version.
Update: I updated iOS to 14.5.1 but nothing changed.
Share Improve this question edited Oct 3, 2021 at 10:53 ADTC 10.2k6 gold badges76 silver badges99 bronze badges asked May 5, 2021 at 13:25 Zakaria M. JawasZakaria M. Jawas 4778 silver badges18 bronze badges 6- That is still experimental I believe – StepPen-codes Commented May 5, 2021 at 13:26
- Oh, i will have to rebuild the whole class then. Thanks – Zakaria M. Jawas Commented May 5, 2021 at 13:27
- caniuse./?search=private%20field this site tells which browsers has it covered – StepPen-codes Commented May 5, 2021 at 13:27
- @StepPen-codes - No, not experimental anymore, but still only just now supported by JavaScriptCore in the wild. – T.J. Crowder Commented May 5, 2021 at 13:31
- 2 You're conflating "private class fields" and "private class methods". Safari now supports private fields but not private methods. caniuse./?search=private%20class – huyz Commented Sep 16, 2021 at 14:45
3 Answers
Reset to default 4On iOS, even Chrome uses Apple's JavaScriptCore JavaScript engine instead of its usual V8 (and uses the WebKit rendering engine, instead of the usual Blink) because of Apple policies¹ (this is true of all other browsers as well).
JavaScriptCore only just added support for private fields in iOS 14.5, so you'll need to be on a pletely up-to-date device (as of this writing). The proposal is no longer "in progress," it's in the specification (as of the last TC39 meeting).
¹ They can't use their own JavaScript engines because that would require allocating executable memory, which Apple only allows Apple apps to do. (V8 did add an interpreter-only mode, but JavaScriptCore is quite a good JavaScript engine and the iOS Chrome folks just use it instead.) They can't use their own rendering engines because Apple doesn't want them to, you have to use their WebView ponent instead.
JS Private Field is supported in Safari & Chrome IOS 14.5 (released 10 days ago).
Safari & Chrome in IOS must be updated to 14.5 for the Class Private fields to work
Click the link below for more information in browser patibilities,
https://caniuse./?search=private%20field
What you wrote there, i.e. #privateFunction()
, is a private method, not a private field.
Private methods are unsupported in Safari older than version 15
At the time of writing, this is the browser support in MDN according to the Private class features page:
Because private class methods are not supported by any existing Safari version, it didn't work for you on iOS 14.5.1 either.
Since iOS devices remain very popular, my advice at this time would be to avoid using most (if not all) of these features listed above until such time in the future when browser patibility improves.
The good news is that Safari 15 on iOS 15 and macOS 12 supports private class methods and accessors, according to the official release notes.