Es hängt davon ab, wo und wie die Funktion ist (oder nicht) erklärt.
Wenn es ein globale und nicht über let name = ...
oder const name = ...
Syntax erklärt (und es ist nicht eine Klasse Konstruktor mit class
deklarierte), können Sie mit der Suche nach ihm als Eigenschaft auf dem globalen Objekt überprüfen können. (Diese Vorbehalte sind alles ES2015-Dinge, mehr unten.) Sie können einen Verweis auf das globale Objekt über this
im loose-Modus auf globaler Ebene erhalten; Browser geben Ihnen auch einen globalen Namen window
. So unter der Annahme eines Browser:
if (typeof window[func_name] === "function") {
// ....
}
Wenn es könnte nicht ein global sein, sondern nur in ihrem Umfang, weil Ihr Code über sie schließt, oder wenn es erstellt wurde, eine jener ES2015 Mechanismen bereits erwähnt, gibt es wirklich keine gute Möglichkeit, andere zu prüfen als eval
:
if (eval("typeof " + func_name) === "function") {
// ....
}
mit eval
ein letzter Ausweg ist, und Sie müssen es nur mit streng gesteuerten Eingang verwenden. Aber wenn Sie müssen und Sie streng kontrollierten Input haben, ist es in Ordnung.
Über die ES2015 Einsprüche:
Die neue let
, const
und class
sind sehr interessant beasties: Wenn Sie im globalen Bereich verwendet wird, schaffen sie Globals, aber sie nicht erstellen Eigenschaften auf das tun globales Objekt. Ab ES2015 sind nicht alle globalen Variablen Eigenschaften des globalen Objekts, obwohl alle Eigenschaften des globalen Objekts globale Variablen sind. Es ist Teil des Versuchs, den stark verschmutzten globalen Namespace zu zügeln und dem JavaScript-Bindungsmodell mehr Sicherheit zu geben. (Jetzt haben wir echte Module.
)
So (beachten Sie, dass dies nur in modernste Browser ausgeführt wird):
// Global scope, in a browser (because I used `window` and `document.body`) that
// implements this aspect of ES2015 (as I write this, Firefox's SpiderMonkey
// doesn't, Chrome's V8 does on the latest Chrome; expect SpiderMonkey and IE
// to catch up pretty quick (didn't test IE Edge, maybe it's already there)
// Strict mode isn't required for this behavior, but for the moment V8 only
// supports the block-scoped constructs in strict mode.
"use strict";
let tbody = setup();
// Old-fashioned var: Creates a property on the global object, so
// we get "function, function"
var f1 = function() { /*...*/ };
result("var declaration", typeof f1, typeof window["f1"]);
// Function declaration: Creates a property on the global object, so
// "function, function"
function f2() {}
result("function declaration", typeof f2, typeof window["f2"]);
// `let` declaration: Doesn't create property on global object, so
// "function, undefined"
let f3 = function() { /*...*/ };
result("let declaration", typeof f3, typeof window["f3"]);
// `const` declaration: Doesn't create property on global object, so
// "function, undefined"
const f4 = function() { /*...*/ };
result("const declaration", typeof f4, typeof window["f4"]);
// `class` declaration: Doesn't create property on global object, so
// "function, undefined"
class C1 {}
result("class declaration", typeof C1, typeof window["C1"]);
function setup() {
document.body.insertAdjacentHTML(
"beforeend",
"<table>" +
"<thead>" +
"<tr><th>test</th><th>global</th><th>prop</th></tr>" +
"</thead>" +
"<tbody></tbody>" +
"</table>"
);
return document.body.querySelector("tbody");
}
function result(label, direct, win) {
tbody.insertAdjacentHTML(
"beforeend",
"<tr><td>" + [label, direct, win].join("</td><td>") + "</td></tr>"
);
}
body {
font-family: sans-serif;
}
table {
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 4px 8px;
}
Ausgabe auf topaktuelle Browser:
+----------------------+------------+-----------+
| test | global | prop |
+----------------------+------------+-----------+
| var declaration | function | function |
| function declaration | function | function |
| let declaration | function | undefined |
| const declaration | function | undefined |
| class declaration | function | undefined |
+----------------------+------------+-----------+
Hinweis: Einige transpilers erzwingen dies nicht streng. Wenn Sie also unterschiedliche Ergebnisse in transpiliertem Code sehen, seien Sie nicht überrascht.
Argumente zu übergeben: window [func_name] (myArg1, myArg2); – mpemburn