[英]Is it possible for a JavaScript function to return its own function call as a string?
在JavaScript中,函數是否可以將自己的函數調用作為字符串返回?
function getOwnFunctionCall(){
//return the function call as a string, based on the parameters that are given to the function.
}
我希望這個函數簡單地將它自己的函數調用作為字符串返回(如果它甚至可以這樣做):
var theString = getOwnFunctionCall(5, "3", /(a|b|c)/);
//This function call should simply return the string "getOwnFunctionCall(5, \"3\", "\/(a|b|c)\/")".
我把這個放在jsFiddle上: http : //jsfiddle.net/pGXgh/ 。
function getOwnFunctionCall() {
var result = "getOwnFunctionCall(";
for (var i=0; i < arguments.length; i++) {
var isString = (toString.call(arguments[i]) == '[object String]');
var quote = (isString) ? "\"" : "";
result += ((i > 0) ? ", " : "");
result += (quote + arguments[i] + quote);
}
return result + ")";
}
alert(getOwnFunctionCall(5, "3", /(a|b|c)/));
請注意,這應該適用於您的示例,但仍需要將任意復雜對象/ JSON作為參數包含在內。
function DisplayMyName()
{
//Convert function arguments into a real array then let's convert those arguments to a string.
var args = [].slice.call(arguments).join(',');
// Get Function name
var myName = arguments.callee.toString();
myName = myName.substr('function '.length);
myName = myName.substr(0, myName.indexOf('('));
return(myName + " ("+ args + ")");
}
var functionText = DisplayMyName(5, "3", /(a|b|c)/) //returns DisplayMyName(5, "3", /(a|b|c)/)
alert(functionText);
使用隱式arguments
變量,您可以提取函數參數和函數名稱:
function getOwnFunctionCall() {
var args = arguments; // Contains the arguments as an array
var callee = arguments.callee; // The caller function
// Use this to construct your string
}
編輯
一些評論指出, callee
不是可以依賴的東西。 但是,如果您要在每個方法中執行此操作,那么只需使用您定義的函數名稱:
var functionName = "getOwnFunctionCall"; // But you can really just use it inline...
如果您需要這樣做,並且需要在全局嚴格中執行,並且您不想對名稱進行硬編碼:
function args(arg){
var me;
try{ badCAll654(); }catch(y){ me=String(y.stack).split("args")[1].split("\n")[1].trim().split("@")[0].replace(/^at /,"").split(" ")[0].trim() }
return me +"("+[].slice.call(arg).join(", ")+")";
}
function getOwnFunctionCall() {
"use strict";
return args(arguments);
}
getOwnFunctionCall(1,true, /dd/);
這可以是一個很好的調試工具,但我不建議在生產網站/應用程序上使用它; 它會對性能產生很大的影響。 此模式僅適用於chrome和firefox,但在全局“use strict”下工作。
IE9不那么嚴格,所以你可以做到以下幾點:
function args(arg){
var me=arg.callee+'';
return me.split("(")[0].split("function")[1].trim() +"("+[].slice.call(arg).join(", ")+")";
}
function getOwnFunctionCall() {
"use strict";
return args(arguments);
}
getOwnFunctionCall(1,true, /dd/);
如果你對trim()進行多重填充,它也應該在IE8中工作。 如果你不使用strict,你可以做更酷的事情,比如記錄調用正在記錄的函數的函數。 如果你想要參數的名稱而不僅僅是值,你甚至可以翻錄該函數的源來查找對已記錄函數的調用。 復雜而毫無價值,但可能。
再次,你應該真的只用於調試!
根據您的評論
我一直試圖找到防止評估eval語句中特定函數的方法,這是解決該問題的一種可能的解決方案。
你要求的可能不是你真正需要的。 為什么不在evaling之前覆蓋你想要阻止的eval
並將其恢復為aferwards:
var blacklist = [ 'alert', 'setTimeout' ];
var old = {};
// Save the blacklisted functions and overwrite
blacklist.forEach(function(name) {
old[name] = window[name];
window[name] = function() {
console.log(name + ' has been disabled for security reasons');
}
});
eval('alert("Hello world")');
// restore the original functions
blacklist.forEach(function(name) {
window[name] = old[name];
});
函數是否可以將自己的函數調用作為字符串返回?
不能。你不能通過你的參數的表達式提取函數 - 你只能訪問它們的值。 當然,您可以使用原始值模擬調用字符串,但您永遠不知道它們是作為變量,文字還是整個表達式傳遞給函數。
也許,Mozilla的toSource
方法可以幫助你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.