samedi 4 avril 2015

Can you turn an anonymous method (function) into a named method dynamically?

Given this method:



var obj = {}, obj.foo = function () {};


Is it possible to assign the method a name after it's created so it looks similar to:



var obj = {}, obj.bar = function bar() {};


The foo method is anonymous and assigned to a property. The bar method is named and assigned to a property.


Can you turn the anonymous foo method into a named foo method dynamically? Is there a property on the function object which can be set or something similar to:



obj.foo.<name> = Object.keys(obj)[0];


Without using jquery, please. Also, this is in a node server application so cross browser issues won't matter.


EDIT: The answer that worked for me is Daniel's link: How to dynamically set a function/object name in Javascript as it is displayed in Chrome. This approach also handles parameters for the function.


Aucun commentaire:

Enregistrer un commentaire