Accediendo al número de línea en JavaScript V8 (Chrome & Node.js)
Los desarrolladores de JavaScript que han pasado tiempo en lenguajes como C a menudo pierden la capacidad de usar ciertos tipos de introspección, como el registro de números de línea y desde qué método se invocó el método actual. Bueno, si estás usando V8 (Chrome, Nodo.js) puede emplear lo siguiente.
49
Author: james_womack, 2012-07-09
1 answers
Object.defineProperty(global, '__stack', {
get: function(){
var orig = Error.prepareStackTrace;
Error.prepareStackTrace = function(_, stack){ return stack; };
var err = new Error;
Error.captureStackTrace(err, arguments.callee);
var stack = err.stack;
Error.prepareStackTrace = orig;
return stack;
}
});
Object.defineProperty(global, '__line', {
get: function(){
return __stack[1].getLineNumber();
}
});
console.log(__line);
Lo anterior se registrará 19
.
Combinado con arguments.callee.caller
puede acercarse al tipo de registro útil que obtiene en C a través de macros.
81
Author: james_womack,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-08 20:35:35
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-08 20:35:35