简体   繁体   English

Javascript 私有 class 字段或方法在 iOS Chrome 或 Z50DE9BC68C93DC32D8C7C90593471760 中不起作用

[英]Javascript private class fields or methods not working in iOS Chrome or Safari

Do Javascript private fields or methods using # work in iOS? Javascript 私有字段或使用#的方法在 iOS 中是否有效?

For example, I have this class:例如,我有这个 class:

Class A { 
    #privateFunction() {return ""} 
}

Creating an instance of class A works in Android and desktop.在 Android 和桌面中创建 class A 的实例。 But in iOS, I get the following error: cannot find variable: A但是在 iOS 中,我收到以下错误: cannot find variable: A

I know the error is related to iOS, because if I made the privateFunction public, it works.我知道该错误与 iOS 有关,因为如果我将privateFunction公开,它就可以工作。

I'm testing the code on a real iPhone device updated to the latest iOS version.我正在更新到最新 iOS 版本的真实 iPhone 设备上测试代码。

Update: I updated iOS to 14.5.1 but nothing changed.更新:我将 iOS 更新为 14.5.1,但没有任何改变。

JS Private Field is supported in Safari & Chrome IOS 14.5 (released 10 days ago). Safari 和 Chrome IOS 14.5(10 天前发布)支持 JS 私有字段。

Safari & Chrome in IOS must be updated to 14.5 for the Class Private fields to work Safari 和 IOS 中的 Chrome必须更新到 14.5 ,Class 私有字段才能工作

Click the link below for more information in browser compatibilities,单击下面的链接以获取有关浏览器兼容性的更多信息,

https://caniuse.com/?search=private%20field https://caniuse.com/?search=private%20field

On 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).在 iOS 上,甚至 Chrome 也使用 Apple 的 JavaScriptCore JavaScript 引擎,而不是通常的 V8(并使用 WebKit 渲染引擎,而不是通常的 Blink),因为 Apple 政策¹(所有其他浏览器也是如此)。

JavaScriptCore only just added support for private fields in iOS 14.5, so you'll need to be on a completely up-to-date device (as of this writing). JavaScriptCore在 iOS 14.5 中添加了对私有字段的支持,因此您需要使用完全最新的设备(在撰写本文时)。 The proposal is no longer "in progress," it's in the specification (as of the last TC39 meeting).该提案不再“进行中”,它在规范中(截至上一次 TC39 会议)。


¹ They can't use their own JavaScript engines because that would require allocating executable memory, which Apple only allows Apple apps to do. ¹ 他们不能使用自己的 JavaScript 引擎,因为这需要分配可执行的 memory,而 Apple 只允许 Apple 应用程序这样做。 (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 component instead. (V8 确实添加了仅解释器模式,但 JavaScriptCore 是一个相当不错的 JavaScript 引擎,而 iOS Chrome 的人只是使用它。)他们不能使用自己的渲染引擎,因为 Apple 不想要他们,你必须改用他们的 WebView 组件。

What you wrote there, ie #privateFunction() , is a private method, not a private field.你在那里写的,即#privateFunction() ,是一个私有方法,而不是一个私有字段。

Private methods are unsupported in Safari. Safari 中不支持私有方法。

At the time of writing, this is the browser support in MDN according to the Private class features page :在撰写本文时, 根据Private class 功能页面,这是 MDN中的浏览器支持:

在此处输入图像描述

Because private class methods are not supported by any existing Safari version, it didn't work for you on iOS 14.5.1 either.由于任何现有的 Safari 版本都不支持私有 class 方法,因此它在 iOS 14.5.1 上也不适用于您。

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 compatibility improves.由于 iOS 设备仍然非常受欢迎,我目前的建议是避免使用上面列出的大部分(如果不是全部)这些功能,直到将来浏览器兼容性得到改善。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM