掌握Reflect.ownkeys实现JS对象属性遍历

需积分: 5 0 下载量 172 浏览量 更新于2024-10-30 收藏 885B ZIP 举报
资源摘要信息: "在JavaScript中, Reflect.ownKeys() 是一个内置的 Reflect 对象的方法,用于获取一个给定对象自身包含的所有属性的键名(不包括原型链上的属性)。这个方法在需要操作对象属性的场景中非常有用,特别是在遍历对象时,可以用来列出对象的所有属性名,而不会获取到从原型链继承的属性。Reflect.ownKeys() 接受一个对象作为参数,并返回一个包含该对象的所有属性名的数组。这个数组通常包含三种类型的键名:字符串类型的键名、符号类型的键名,以及带有getter或setter的属性名。" 为了深入理解 Reflect.ownKeys() 方法,我们需要了解以下几点: 1. **对象属性的类型**: - **字符串属性**:最常见的属性,通常在对象字面量中直接用引号括起来的键名。 - **符号属性**:ES6 引入了一种新的基本类型——符号(Symbol),它用于创建对象的唯一且不可变的标识符。符号属性可以用作对象的键名。 - **访问器属性**:通过 getter 和 setter 方法定义的属性,它们不是用来存储值的,而是用来定义读取或写入值时的行为。 2. **Reflect.ownKeys() 方法的工作原理**: - 当 Reflect.ownKeys() 方法被调用时,它会返回一个数组,该数组包含了传入对象的所有键名。 - 数组中的键名顺序是未定义的,不过在ES6的规范中,字符串键名和符号键名是分开出现的,且符号键名通常会出现在字符串键名之前。 - 如果对象是一个基本包装对象(如 String, Number, Boolean 等),那么返回的数组中会包含一个名为 "length" 的字符串键名,以及任何原型链上的符号键名。 3. **Reflect.ownKeys() 的使用场景**: - **对象属性遍历**:当需要遍历一个对象的所有属性时,使用 Reflect.ownKeys() 可以确保只操作对象自身的属性,不涉及原型链上的属性。 - **数据序列化**:在将对象转换为JSON格式数据时,通常不希望包含原型链上的属性,可以使用 Reflect.ownKeys() 来获取对象的键名,再进行序列化。 - **保护原型链属性**:如果开发者不希望对象的属性被外部代码修改,可以使用 Reflect.ownKeys() 来确保这些属性不被遍历到。 4. **与相关方法的比较**: - **Object.keys()**:此方法只返回对象上可枚举的字符串属性键名。 - **Object.getOwnPropertyNames()**:此方法返回对象上所有的字符串属性键名,包括不可枚举的属性,但不包括符号键名。 - **Object.getOwnPropertySymbols()**:此方法返回对象上所有的符号属性键名,但不包括字符串键名。 5. **代码示例**: 下面是一个使用 Reflect.ownKeys() 方法的简单代码示例,该示例展示了如何遍历对象的所有自身属性: ```javascript const myObject = { name: 'Alice', age: 30, [Symbol.for('mySymbol')]: 'mySymbolValue', get info() { return this.name + this.age; } }; Reflect.ownKeys(myObject).forEach(key => { console.log(key); // 输出属性名,包括符号属性名 }); console.log('---'); for (const key of Object.keys(myObject)) { console.log(key); // 输出可枚举的字符串属性名 } console.log('---'); for (const key of Object.getOwnPropertyNames(myObject)) { console.log(key); // 输出字符串属性名,包括不可枚举的 } console.log('---'); for (const key of Object.getOwnPropertySymbols(myObject)) { console.log(key); // 输出符号属性名 } ``` 通过上述知识点的介绍和代码示例,可以看出 Reflect.ownKeys() 在JavaScript对象操作中的重要性和实用性,它为开发者提供了更多控制和获取对象自身属性的手段。

01-01 08:11:27.038 E/StrictMode( 2641): Tried to access the API:ViewConfiguration which needs to have proper configuration from a non-UI Context:com.android.camera.app.CameraApp@32d9eff The API:ViewConfiguration needs a proper configuration. Use UI contexts such as an activity or a context created via createWindowContext(Display, int, Bundle) or createConfigurationContext(Configuration) with a proper configuration. 01-01 08:11:27.038 E/StrictMode( 2641): java.lang.IllegalAccessException: Tried to access the API:ViewConfiguration which needs to have proper configuration from a non-UI Context:com.android.camera.app.CameraApp@32d9eff 01-01 08:11:27.038 E/StrictMode( 2641): at android.os.StrictMode.assertConfigurationContext(StrictMode.java:2296) 01-01 08:11:27.038 E/StrictMode( 2641): at android.view.ViewConfiguration.get(ViewConfiguration.java:521) 01-01 08:11:27.038 E/StrictMode( 2641): at android.view.View.<init>(View.java:5321) 01-01 08:11:27.038 E/StrictMode( 2641): at android.widget.ImageView.<init>(ImageView.java:181) 01-01 08:11:27.038 E/StrictMode( 2641): at com.android.camera.data.PhotoItem.getView(PhotoItem.java:127) 01-01 08:11:27.038 E/StrictMode( 2641): at com.android.camera.data.CameraFilmstripDataAdapter.getView(CameraFilmstripDataAdapter.java:151) 01-01 08:11:27.038 E/StrictMode( 2641): at com.android.camera.widget.FilmstripView.buildViewItemAt(FilmstripView.java:850) 01-01 08:11:27.038 E/StrictMode( 2641): at com.android.camera.widget.FilmstripView.reload(FilmstripView.java:1827) 01-01 08:11:27.038 E/StrictMode( 2641): at com.android.camera.widget.FilmstripView.access$1300(FilmstripView.java:58) 01-01 08:11:27.038 E/StrictMode( 2641): at com.android.camera.widget.FilmstripView$3.onFilmstripItemLoaded(FilmstripView.java:1567) 01-01 08:11:27.038 E/StrictMode( 2641): at com.android.camera.data.CameraFilmstripDataAdapter.replaceItemList(CameraFilmstripDataAdapter.java:276) 01-01 08:11:27.038 E/StrictMode( 2641): at com.android.camera.data.CameraFilmstripDataAdapter.access$700(CameraFilmstripDataAdapter.java:39) 01-01 08:11:27.038 E/StrictMode( 2641): at com.android.camera.data.CameraFilmstripDataAdapter$QueryTask.onPostExecute(CameraFilmstripDataAdapter.java:447) 01-01 08:11:27.038 E/StrictMode( 2641): at com.android.camera.data.CameraFilmstripDataAdapter$QueryTask.onPostExecute(CameraFilmstripDataAdapter.java:379) 01-01 08:11:27.038 E/StrictMode( 2641): at android.os.AsyncTask.finish(AsyncTask.java:771) 01-01 08:11:27.038 E/StrictMode( 2641): at android.os.AsyncTask.access$900(AsyncTask.java:199) 01-01 08:11:27.038 E/StrictMode( 2641): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:788) 01-01 08:11:27.038 E/StrictMode( 2641): at android.os.Handler.dispatchMessage(Handler.java:106) 01-01 08:11:27.038 E/StrictMode( 2641): at android.os.Looper.loopOnce(Looper.java:201) 01-01 08:11:27.038 E/StrictMode( 2641): at android.os.Looper.loop(Looper.java:288) 01-01 08:11:27.038 E/StrictMode( 2641): at android.app.ActivityThread.main(ActivityThread.java:7870) 01-01 08:11:27.038 E/StrictMode( 2641): at java.lang.reflect.Method.invoke(Native Method) 01-01 08:11:27.038 E/StrictMode( 2641): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 01-01 08:11:27.038 E/StrictMode( 2641): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

2023-07-14 上传

Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@18e8e27b] 2023-07-22 09:35:58.167 ERROR 44888 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: com.sc_core.hardware.entity.Bracelet cannot be cast to com.sc_core.hardware.entity.BraceletMessage] with root cause java.lang.ClassCastException: com.sc_core.hardware.entity.Bracelet cannot be cast to com.sc_core.hardware.entity.BraceletMessage at com.sc_core.hardware.service.Impl.BraceletServiceImpl.BraceletOnlineCount(BraceletServiceImpl.java:46) ~[classes/:na] at com.sc_core.hardware.controller.BraceletController.BraceletOnlineCount(BraceletController.java:37) ~[classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131] at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:652) ~[tomcat-embed-core-9.0.46.jar:4.0.FR] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.46.jar:4.0.FR]

2023-07-23 上传

Process: com.android.systemui, PID: 12501 C1C4B8B 06-09 16:11:31.132 12501 12501 E AndroidRuntime: java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x4200010 (has extras) } in com.android.systemui.qsview.signal.SignalUtils$1@dbb3275 C1C4B8B 06-09 16:11:31.132 12501 12501 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$android-app-LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1830) C1C4B8B 06-09 16:11:31.132 12501 12501 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args$$ExternalSyntheticLambda0.run(Unknown Source:2) C1C4B8B 06-09 16:11:31.132 12501 12501 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:942) C1C4B8B 06-09 16:11:31.132 12501 12501 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99) C1C4B8B 06-09 16:11:31.132 12501 12501 E AndroidRuntime: at android.os.Looper.loopOnce(Looper.java:201) C1C4B8B 06-09 16:11:31.132 12501 12501 E AndroidRuntime: at android.os.Looper.loop(Looper.java:288) C1C4B8B 06-09 16:11:31.132 12501 12501 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:8061) C1C4B8B 06-09 16:11:31.132 12501 12501 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) C1C4B8B 06-09 16:11:31.132 12501 12501 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:703) C1C4B8B 06-09 16:11:31.132 12501 12501 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911) C1C4B8B 06-09 16:11:31.132 12501 12501 E AndroidRuntime: Caused by: java.lang.IllegalStateException: Pid 12501 has exceeded the number of permissible registered listeners. Ignoring request to add.

2023-06-13 上传

06-08 10:58:36.121 1815 1815 E AndroidRuntime: Process: com.android.settings, PID: 1815 06-08 10:58:36.121 1815 1815 E AndroidRuntime: java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.supplicant.STATE_CHANGE flg=0x10 (has extras) } in com.android.settings.m8settings.receiver.WifiReceiver@41c8a5c 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$android-app-LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1830) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args$$ExternalSyntheticLambda0.run(Unknown Source:2) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:942) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at android.os.Looper.loopOnce(Looper.java:201) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at android.os.Looper.loop(Looper.java:288) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:8061) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:703) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.wifi.SupplicantState.name()' on a null object reference 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at com.android.settings.m8settings.receiver.WifiReceiver.onReceive(WifiReceiver.java:46) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$android-app-LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1820) 06-08 10:58:36.121 1815 1815 E AndroidRuntime: ... 9 more

2023-06-09 上传

怎么报错应该怎么解决java.lang.IllegalArgumentException: View=com.xiaopeng.xui.widget.XLinearLayout{6842348 V.E...... ......ID 0,0-600,130} not attached to window manager 05-26 17:48:27.970 10708 10708 E AndroidRuntime: at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:543) 05-26 17:48:27.970 10708 10708 E AndroidRuntime: at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:447) 05-26 17:48:27.970 10708 10708 E AndroidRuntime: at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:196) 05-26 17:48:27.970 10708 10708 E AndroidRuntime: at com.xiaopeng.systemui.speech.component.asr.AsrAreaWidget.onAsrHide(AsrAreaWidget.java:50) 05-26 17:48:27.970 10708 10708 E AndroidRuntime: at com.xiaopeng.systemui.speech.model.AsrModel.notifyChanged(AsrModel.java:85) 05-26 17:48:27.970 10708 10708 E AndroidRuntime: at com.xiaopeng.systemui.speech.model.AsrModel.access$100(AsrModel.java:15) 05-26 17:48:27.970 10708 10708 E AndroidRuntime: at com.xiaopeng.systemui.speech.model.AsrModel$1.onInputText(AsrModel.java:73) 05-26 17:48:27.970 10708 10708 E AndroidRuntime: at com.xiaopeng.systemui.speech.presenter.SpeechManager$2.lambda$onInputText$0$SpeechManager$2(SpeechManager.java:172) 05-26 17:48:27.970 10708 10708 E AndroidRuntime: at com.xiaopeng.systemui.speech.presenter.-$$Lambda$SpeechManager$2$LNEIprveqAbFGXR19BN2ru0Bj2o.run(Unknown Source:4) 05-26 17:48:27.970 10708 10708 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:938) 05-26 17:48:27.970 10708 10708 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99) 05-26 17:48:27.970 10708 10708 E AndroidRuntime: at android.os.Looper.loopOnce(Looper.java:232) 05-26 17:48:27.970 10708 10708 E AndroidRuntime: at android.os.Looper.loop(Looper.java:334) 05-26 17:48:27.970 10708 10708 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7985) 05-26 17:48:27.970 10708 10708 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 05-26 17:48:27.970 10708 10708 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 05-26 17:48:27.970 10708 10708 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013)

2023-05-27 上传