1

I'm trying to learn on making my first dynamic loader from scratch with jQuery (for learning purpose) here is where I got stuck:

<script type="text/javascript">
jQuery.learningdynamicloader= function(){
     var js = [];
     var css = [];
     console.log(js,css);
};
</script>
<script>
$(document).ready(function() {
    $.learningdynamicloader(function(){
        js = ['jquery','test'];
        css = ['css','test'];
    });
});
</script>

My firebug log js and css returns nothing, it seems that there is a problem. After I Google is there any diffrence between js = ['values'] and js : ['values'] ?

Thanks for looking.

2 Answers 2

5

Try passing the arrays as arguments rather than declaring them locally:

jQuery.learningdynamicloader= function(js, css){
    console.log(js,css);
};

And then:

var js = ['jquery','test'];
var css = ['css','test'];
$.learningdynamicloader(js, css);

Edit: I think that what you are after is something like this though:

jQuery.learningdynamicloader= function(params){
    console.log(params["js"], params["css"]);
};

And then:

$(document).ready(function() {
    $.learningdynamicloader({ "js": ['jquery','test'], "css": ['css','test'] });
});

This will pass complex object to your function, with two properties, each array of its own.

Sign up to request clarification or add additional context in comments.

3 Comments

this work great!, yeah im aware about passing them as arguments, but its better on readability, as most of plugin example use something like this. just following some existing jquery function like api.jquery.com/jQuery.ajax they can have local vars, how do they achieve this ?
Can you show an example of code that does this? I'm not sure what you're after.
Cheers, regarding what you said about .ajax() see my edit, this is pretty similar, passing whole bunch of properties as one complex object to the function.
1

Sorry to say this go back and read into javascript, you cant overide a function by passing it a function as an argument

jQuery.learningdynamicloader= function(fn){
     js = [];
     css = [];
     fn();
     console.log(js,css);
};

The same can be done like so this one will use the function private scope the top one will use the window scope

jQuery.learningdynamicloader= function(fn){
         var js = [];
         var css = [];
         fn(js, css);
         console.log(js,css);
    };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.