私はhereから次のスクリプトを持っている:`if(document.createEvent){..}`関数の呼び出しはかっこなしで呼び出すのですか?
function download(filename, text) {
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
pom.setAttribute('download', filename);
if (document.createEvent) {
var event = document.createEvent('MouseEvents');
event.initEvent('click', true, true);
pom.dispatchEvent(event);
}
else {
pom.click();
}
}
download('test.txt', 'Hello world!');
今私はif (document.createEvent)
の使用方法を理解ドント:
私は質問In JavaScript, does it make a difference if I call a function with parentheses?のための答えを読んで、他のいくつかの質問をお読みください。
ほとんどの例では、window.onload = initAll;
またはvar ret = Multiply;
のような参照がありますが、ここではそうではありません。私にとって最も興味深い例があった。ここで
function Multiply(operator, operand) {
return operator * operand;
}
var operator = 3;
var operand = 4;
var ret = Multiply;
、著者によると、Multiply
が実行され、機能Multiply
からret
参照されません。
here括弧なしで関数を呼び出すことは参照であると言われています。
しかし、私には、イベントを作成する関数であるdocument.createEvent
への参照が意味をなさないので、私はさらに混乱してしまいます。
document.createEvent()
のようなドキュメントは、hereのように、パラメータなしの使用法を指摘していません。
だから、私の脳の闇に光をあげてください。 私はあなたの助けに感謝します。
質問(ショート):
- は、関数呼び出しまたは参照
document.createEvent
ですか? document.createEvent
は何を返しますか?
document.createEvent関数が存在するかどうかを確認しています。それはまったくそれを呼んでいない。 JavaScript関数ではファーストクラスのオブジェクトであり、そのように扱うことができます。 –
ブラウザが 'document.createEvent'をサポートしているかどうかをチェックしています。そうでない場合は別の方法を使用します。 – deceze
オペレータのない[JavaScriptのIfステートメント条件の重複の可能性がありますか?それは何ですか?](http://stackoverflow.com/questions/3494614/javascript-if-statement-condition-with-no-operator-what-does-it-do) - これはまったく同じテクニックです。 – Xufox