javascript - How does js call a method defined in an object through a variable and pass parameters?
typecho
typecho 2017-06-12 09:32:18
0
1
578

I have defined several methods now and receive a parameter. When accessing a method in this object, how do I access it through a variable?

Definition method:

import moment from 'moment'
let customFun = {
    blockRenderFun: {
        A: function (value) {
            return 'a' + value
        },
        B: function (value) {
            return 'b' + value
        },
        C: function (value) {
            return moment(value).format('YYYY-MM-DD hh:mm:ss')
        }
    }
}
export default customFun

Calling method:

import customFun from '~/function-modules.js'
let renderSourceArray = ['A','B','C']
let value = 123456
...
customFun.blockRenderFun[renderSourceArray[2]](value)   // 此种写法正确,问题关闭                             

It is known that the properties and methods of the access object can be accessed through "." or "['']"

typecho
typecho

Following the voice in heart.

reply all(1)
阿神

return a + value
Where did you define a and declare it?
Is this possible without reporting an error?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template