简体   繁体   中英

Get name of property from its own function

I would like to get the name of a property from within its own function. My current approach does not work because the function is nameless. How can I do this?

window.APP = {
    models: {
        ex_model: kendo.observable({
            ex_property: function () {
                var property_name = arguments.callee.name.toString();
                console.log(property_name);
            },
        }),
    }
}    

Thank you.

You can make ex_property have a name. Instead of using function() , you can say function function_name() , and then arguments.callee.name.toString() would return function_name . Like this:

window.APP = {
    models: {
        ex_model: kendo.observable({
            ex_property: function function_name() {
                var property_name = arguments.callee.name.toString();
                console.log(property_name); // will return function_name
            },
        }),
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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