简体   繁体   中英

How do I get chrome's console to display a function with its dynamically changed name?

When I do console.log on a function in chrome, it displays it using the function's original name, not the name at the time I called console.log. For example:

var someFunction = function genericFunction() {}
Object.defineProperty(someFunction, 'name', {writable: true});
someFunction.name = 'specificFunction';
console.log(someFunction);

in chrome 43 returns

function genericFunction()

which I find confusing. I'd expect to see

function specificFunction()

Where is it finding the old name of the function? Is there some other property which I need to change to get chrome to accept the new name?

If something in the function throws an error, then chrome's stack trace does display the new function name. This is what I'd expect, but seems inconsistent with the console.log behaviour. Am I missing something?

you'll slap yourself ...

console.log(someFunction.name)

I hope you realise you wont be able to call the function using

genericFunction()

if you want to "alias" (for want of a better term) someFunction to genericFunction, simply

var genericFunction = someFunction;

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