4

Currently re-learning JS because I stopped JS for a while. ES6, wtf? Can someone explain the difference between the different ways of creating functions, and the difference between let and var. please? (Especially the function thing that’s got be fucked up)

Comments
  • 2
    There's youtube video explaining something about JS (3 hours long)
    https://youtube.com/watch/...
  • 2
    About let vs var:
    // var
    for (var i = 0; i < 10; i++);
    console.log(i); // will print last assignment of I

    // let
    for (let i = 0; i < 10; i++);
    console.log(i); // error
  • 2
    let declares variable in it's own scope and that variable isn't visible outside.
  • 2
    {
    let i = 1;
    console.log(i); // 1
    // new scope
    {
    let i = 2;
    console.log(i); // 2
    }
    console.log(i); // 1
    }
  • 2
    {
    var i = 1;
    console.log(i); // 1
    {
    var i = 2; // will replace I that was declared before
    console.log(i); // 2
    }
    console.log(i); // 2
    }
  • 1
    @irene I didn't watched him myself yet.
  • 1
    let is simply a way to declare variables with strict scoping.
  • 1
    The () => {
    //stuff
    }

    is just a shorthand to create functions. That is very useful for callbacks, now you can simply write

    someFunction(() => {
    //callback
    });

    instead of

    someFunction(function() {
    //callback
    }});
  • 3
    Function:

    function Obj () { console.log(this); }
    new Obj(); // his "this" points to himself.

    Arrow function:

    // creates anonymous function, you can't give him a name with arrow statement
    var Obj = () => { console.log(this); }
    new Obj(); // error, Obj is not a constructor
    Obj(); // this points to Window or outer scope.
  • 2
    And arrow function doesn't have his own arguments list.
  • 1
  • 2
    You'll love it once get used to it, I promise
  • 0
  • 1
    @Pogromist that 'var'-example: Javascript does THAT? Literally every other language does proper variable scoping. Like, what the hell?
  • 0
    @ihatecomputers That joke took me a bit to resolve.
  • 1
    @PrivateGER Oops, I didn't mean to make a joke. I see that I missed a word... Is the joke that JS programs crash and burn every time you make a typo?
  • 1
    @ihatecomputers I took the use of "promise" as a joke. :D
  • 1
    @PrivateGER Haha, right 😀
Add Comment