1

I have the following script from here:

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!');

Now I don´t understand the usage of if (document.createEvent):

I read the answer for the question In JavaScript, does it make a difference if I call a function with parentheses? and read some other questions.

In most examples is some kind of referencing like window.onload = initAll; or var ret = Multiply; but thats obviously is not the case here. The most interesting example for me was:

function Multiply(operator, operand) {
    return operator * operand;
}
var operator = 3;
var operand = 4;
var ret = Multiply;

Here, according to the author, Multiply does not get executed and ret reference to the function Multiply.

Also here it is stated that calling a function without parentheses is a reference.

But thats get me confused even more, because for me a reference to document.createEvent, which is a function to create an event, does not make sense.

Documentations about the function document.createEvent() like here does not point out the usage without parameters.

So, please bring some light to the darkness of my brain. I appreciate your help.

Questions (short):

  • Is document.createEvent a function call or a reference?
  • What does document.createEvent return?
Community
  • 1
  • 1
goulashsoup
  • 2,639
  • 2
  • 34
  • 60
  • 1
    It's checking whether the document.createEvent function exists. It's not calling it at all. In Javascript functions are first-class objects and can be treated as such. – jeff carey Sep 28 '16 at 14:22
  • 1
    It's checking whether the browser supports `document.createEvent` and uses it if so, otherwise uses a different approach. – deceze Sep 28 '16 at 14:24
  • Possible duplicate of [JavaScript If statement condition with no operator? What does it do?](http://stackoverflow.com/questions/3494614/javascript-if-statement-condition-with-no-operator-what-does-it-do) — it’s precisely the same technique. – Sebastian Simon Sep 28 '16 at 14:26

3 Answers3

3

Is document.createEvent a function call or a reference?

It is an attempt to access a property on the document object.

That property might exist. It might have a value. That value might be a reference to a function.

If all of those are the case, then it will be a true value (and the if block will run). If it doesn't exist, it will be a false value (and the else block will run).

This is a test to see if the browser supports the function.

What does document.createEvent return?

The value of the property. If the browser supports createEvent then it will evaluate as the function, which is a true value.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

Is document.createEvent a function call or a reference?

It's an expression denoting a property of the document object. One expects its value to be reference to a Function object that will create an event, but it may also be undefined.

What does document.createEvent return?

As an expression, it evaluates to the current value of said property.

OrangeDog
  • 36,653
  • 12
  • 122
  • 207
1

1 - JavaScript has truthy and falsy values. Values such as 0, "", undefined, null are falsy values, while 1, -1, or "text", and for example an existing properties of an object are truthy. (if their values are not falsy!) 2- since createEvent is a property of document object, the code snippet basically tests the existance of that function, which means existance of it is something truthy. I assume this check is related to the browser compatibility & method deprecation. you can read the details from here

erdysson
  • 1,450
  • 13
  • 17
  • I decided that this answer is the best, because it references to what kind of return value is acceptable to let an if-statement evaluate "true". But I upvoted the other answers to, because there are all good. Thanks. – goulashsoup Sep 28 '16 at 14:38