dcsimg
www.webdeveloper.com
Results 1 to 7 of 7

Thread: Explanation of an example

  1. #1
    Join Date
    Mar 2017
    Posts
    5

    Explanation of an example

    Guys iam trying some examples , working with scopes etc. My question here is when it gets into the for loop the i = 0 , so it let j = 0 , and then its pushing into the array the function BUT its not execute it yet . When it finish the i = 1 and the j now is 0 right ? the same til the iteration finish . Now when we call the function it return an array with 3 functions . When i invoke the function in the array the j the three functions shoulnt be 3 ?

  2. #2
    Join Date
    Mar 2017
    Posts
    5

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    4,974
    If you are having a problem with your code, please post a copy of the code here in the forum thread, please use Forum BBCode tags (see mu signature for tags), the problem with past bon sites is that codes get updated and people with similar problems can see how the code was altered through the posts made to the forum. It is also helpful to getting an answer quicker.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  4. #4
    Join Date
    Mar 2017
    Posts
    5

    code

    PHP Code:
    var arr = [];

    function 
    build(){
    for (var 
    03i++) {
      
    let j i;
      
    console.log('j = ' ' i ' i);
      
    arr.push(function(){
        
    console.log(j);
      });

    }

        return 
    arr;
    }


    var 
    newArr build();
    newArr[0]();
    newArr[1]();
    newArr[2](); 

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    4,974
    Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

    take out the let statement. Your loop is for 3 items, 0, 1, 2 = 3 items, if you use i <=3 then you would have 0, 1, 2, 3 in the loop making 4 elements.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  6. #6
    Join Date
    Dec 2012
    Posts
    1,446
    Funny stuff! Till now I did it this way:
    Code:
            var arr = [];
    
            function build() {
                for (var i = 0; i < 3; i++) {
                    arr.push(function (idx) {
                        return function () {
                            console.log(idx);
                        }
                    }(i));
                }
                return arr;
            }
    
            var newArr = build();
            newArr[0]();
            newArr[1]();
            newArr[2]();
    Much easier when using "let".

  7. #7
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    545
    ** deleted by op**

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles