oop - Setting function name (for logging) when using prototype in javascript -


I am using prototype to define an object in javascript.

For example.

  Listerly.prototype.init = function () {this.storage = new listListorage (); this.mainView = new ListerlyMainView (); }   

Everything works fine, but I am also trying to limit my console logging using the following simple logging method:

  Listerly.prototype.log = function (message) {if (this.loggingEnabled) {If (logic & amp; amp; and & amp; amp; arguments.callee & amp; amp; arguments; callee.caller) {var methodName = arguments.callee.caller.name; console.log ("method name:" + methodName); If (this.logMethods ["*"] || this.logMethods [methodName]) {if (methodName) console.log (methodName + ":" + message); Else console.log (message); }} And {console.log (message); }}};   

The problem is that the functions of the function defined by the use of prototype do not have a name, so arguments.callee.caller.name is empty.

Is there a clean way to set the name of the function? I have tried to complete it again through prototype after completing the .name field on each function and setting it, but it does not seem to work.

An ugly solution, which pollutes the global name space by doing this:

  function listerly_finishedLoadingUser (user) {this.user = user; This.mainView.setUser (user); } Listerly.prototype.finishedLoadingUser = listerly_finishedLoadingUser;   

What was the hope that the cleaner's way?

:

  Listerly.prototype.finitedloadingUser = function listerly_prototype_finished User (user) {// รข ?? |};   

You can use the function names within the function to replicate in link mode, where.

Note that the identifier is also the old (Borken) JScript version, so choose it wisely as well as there may be an (unspecified) range for the identifier's length.

(read-only) name property that you have used is unavailable, you may be able to get the function name from function realization, i.e., Function to convert the identifier part into string and extract form (as I did):

  / ** * names a function * * @ param {Function} string} aufonction @return {string} * Name the function if it has it; Empty string otherwise * / GetFunctionName: function (aFunction) {/ * return empty string to zero or undefined * / return (aFunction! = Null & amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; type; auff.name ! = "Undefined" & amp; amp; amp; amp; amp; amp; afunction.name) || (String (function) .mail (/ ^ \ s * function \ s + ([A-Za-z _] \ w *) /)) [["," "]) [1];},   

Also note that the property of caller logic < / Code> object and function example ownership.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -