dimanche 26 juin 2016

Javascript examples, clarify the langue pattern design for functions with-in functions


I am learning javaScript and becoming confused by the logic of the code examples.From codecademy. Why are there function set-ups in function calls. I'm quite confused I am moving from a simplified C -like langue.

The javascript example

var main = function(){
    $('.article').click(function(){
        $('.description').hide();

       $(this).children('.description').show();
    });
    };

My understanding. - main is a function name with a return type of var.

  • $('.article') is a element/object/or class object.

  • .click() is a call to a member function

  • But ???

    .click(function(){ $('.description').hide(); $(this).children('.description').show(); });

*-- This seems to be a newly on the spot created function to run When/If click() is activated or run.

The way I'm used to thinking is like this.

 var *p_obj = $('.article');
    var *p_obj = $('.description');

    var do_click()
    {
     p_obj2.hide();
     p_obj.children(p_obj2).show();
    }

    var main(){
    p_obj.click(do_click);
    }

Function main() looks at p_obj and calls click() .

Click() evaluates to true/false and run the pointer_to function do_click().

Function do_click() looks at the p_obj2 and calls hide() , which preforms an action of hiding the p_obj2.

Function do_click() also looks at p_obj and uses children to scope focus to p_obj2, then it runs show(), which preforms an action of displaying p_obj2.

I do realize my c -like example is wrong and odd. I realize my terminology is wrong or otherwise used incorrectly.

The way this is design seems like I must write extended functionality on-the-spot for every call to .click(), so if-then .click() is run on 3 different items, I'm creating different extended functionality for each object. But I would normally create a single function that varies it's internal execution based on the object or condition click() calls it by.

This set-up seems alright if the code a relatively simple or short, but on-the-spot functional seems like overworking for longer code and code where the functionality repeats but the objects change.

I'm I think about javascript functions with-in functions correctly and is this a design goal of the langue to add long repeating extended functions with-in functions?


Aucun commentaire:

Enregistrer un commentaire