这种语法在JavaScript(ES6可能)中意味着什么:
const {} = variablename;
我目前正试图抓住React。在很多例子中,我遇到了这种语法。例如:
const {girls, guys, women, men} = state;
答案 0 :(得分:16)
首先,这与React无关。它是ECMAScript 6的一部分(如果你愿意,可以是JavaScript 2015)。
您在此处看到的内容称为Destructuring assignment:
02-15 09:51:25.571 16112-16112/? E/Zygote: MountEmulatedStorage()
02-15 09:51:25.571 16112-16112/? E/Zygote: v2
02-15 09:51:25.571 16112-16112/? I/libpersona: KNOX_SDCARD checking this for 10375
02-15 09:51:25.571 16112-16112/? I/libpersona: KNOX_SDCARD not a persona
02-15 09:51:25.571 16112-16112/? I/SELinux: Function: selinux_compare_spd_ram, SPD-policy is existed. and_ver=SEPF_SAMSUNG-SM-N910A_5.1.1 ver=38
02-15 09:51:25.581 16112-16112/? I/SELinux: Function: selinux_compare_spd_ram , priority [1] , priority version is VE=SEPF_SAMSUNG-SM-N910A_5.1.1_0038
02-15 09:51:25.581 16112-16112/? E/Zygote: accessInfo : 0
02-15 09:51:25.581 16112-16112/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
02-15 09:51:25.581 16112-16112/? I/art: Late-enabling -Xcheck:jni
02-15 09:51:25.611 16112-16112/com.firsttread.anthony.soundscope D/TimaKeyStoreProvider: TimaSignature is unavailable
02-15 09:51:25.611 16112-16112/com.firsttread.anthony.soundscope D/ActivityThread: Added TimaKeyStore provider
02-15 09:51:25.691 16112-16112/com.firsttread.anthony.soundscope D/SecWifiDisplayUtil: Metadata value : none
02-15 09:51:25.761 16112-16112/com.firsttread.anthony.soundscope D/PhoneWindow: *FMB* installDecor mIsFloating : false
02-15 09:51:25.761 16112-16112/com.firsttread.anthony.soundscope D/PhoneWindow: *FMB* installDecor flags : -2139029248
02-15 09:51:25.851 16112-16112/com.firsttread.anthony.soundscope D/DatabaseHelper: DatabaseHelper instance successful
02-15 09:51:26.041 16112-16197/com.firsttread.anthony.soundscope D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-15 09:51:26.061 16112-16112/com.firsttread.anthony.soundscope D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
02-15 09:51:26.061 16112-16112/com.firsttread.anthony.soundscope D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
02-15 09:51:26.081 16112-16112/com.firsttread.anthony.soundscope D/SRIB_DCS: log_dcs ThreadedRenderer::initialize entered!
02-15 09:51:26.081 16112-16197/com.firsttread.anthony.soundscope I/Adreno: EGLInit: QTI Build: 07/16/15, 126f54a, If3804f16ae
02-15 09:51:26.091 16112-16197/com.firsttread.anthony.soundscope I/OpenGLRenderer: Initialized EGL, version 1.4
02-15 09:51:26.101 16112-16197/com.firsttread.anthony.soundscope D/OpenGLRenderer: Get maximum texture size. GL_MAX_TEXTURE_SIZE is 16384
02-15 09:51:26.101 16112-16197/com.firsttread.anthony.soundscope D/OpenGLRenderer: Enabling debug mode 0
02-15 09:51:26.251 16112-16112/com.firsttread.anthony.soundscope I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@1c5a7d02 time:217638745
02-15 09:51:27.451 16112-16112/com.firsttread.anthony.soundscope D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
02-15 09:51:27.521 16112-16112/com.firsttread.anthony.soundscope I/Timeline: Timeline: Activity_launch_request id:com.firsttread.anthony.soundscope time:217640012
02-15 09:51:27.551 16112-16112/com.firsttread.anthony.soundscope D/PhoneWindow: *FMB* installDecor mIsFloating : false
02-15 09:51:27.551 16112-16112/com.firsttread.anthony.soundscope D/PhoneWindow: *FMB* installDecor flags : -2139029248
02-15 09:51:27.591 16112-16112/com.firsttread.anthony.soundscope D/AndroidRuntime: Shutting down VM
02-15 09:51:27.591 16112-16112/com.firsttread.anthony.soundscope E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.firsttread.anthony.soundscope, PID: 16112
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.firsttread.anthony.soundscope/com.firsttread.anthony.soundscope.application.view.SoundSessionActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.firsttread.anthony.soundscope.application.presenter.mediaplayback.SoundSessionService.setupSounds(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3149)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3248)
at android.app.ActivityThread.access$1000(ActivityThread.java:197)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6872)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.firsttread.anthony.soundscope.application.presenter.mediaplayback.SoundSessionService.setupSounds(java.lang.String)' on a null object reference
at com.firsttread.anthony.soundscope.application.presenter.SoundSessionPresenter.setupSounds(SoundSessionPresenter.java:83)
at com.firsttread.anthony.soundscope.application.view.SoundSessionActivity.setup(SoundSessionActivity.java:86)
at com.firsttread.anthony.soundscope.application.view.SoundSessionActivity.onCreate(SoundSessionActivity.java:67)
at android.app.Activity.performCreate(Activity.java:6550)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3102)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3248)
at android.app.ActivityThread.access$1000(ActivityThread.java:197)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6872)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
在学习React时,你可能会遇到类似的模式:
const {girls, guys, women, men} = state;
// Is the same as
const girls = state.girls;
const guys = state.guys;
const women = state.women;
const men = state.men;
在这种情况下,您有一个名为import { methodA, methodB } from "my-module";
的模块,它正在公开某些功能。使用my-module
语法,您可以选择要导入的函数。请注意,这不是解构赋值,尽管它的工作方式类似。