1
$.plugin = {

    loadedcss   : [],

    add: function(params) { 
        $.each(params["css"], function(key,value) { 
            // this.loadedcss error undefined
            this.loadedcss.push(value);
        });
    },

    end: function(){
        //console.log(this.loadedcss);
    }
};

as you can see im trying to push my new css params to loadedcss variable. and it says error undefined this.loadedcss maybe there is something that i miss

$.plugin.add({
    css: ['framework.css','csstest.css']
}); 

$.plugin.add({
    css: ['framework.css','csstest.css']
}); 

    $.plugin.end();

how can we push my params['css'] to this.loadedcss ?

3
  • It works for me. jsfiddle.net/fxsUf Commented Aug 4, 2011 at 14:00
  • sorry updated i miss the real code. Commented Aug 4, 2011 at 14:04
  • BTW, you have css and js mixed up in your code. Commented Aug 4, 2011 at 14:09

3 Answers 3

2

You could try,

$.plugin = (function(){
var methods = {
   loadedcss   : [],

    add: function(params) { 
       // IF IN ARRAY NOT LOADED THEN PUSH CSS
      if ($.inArray(params['css'],this.loadedcss) == -1) {
         // error undefined this.loadedcss
         methods.loadedcss.push(params['css']);
       };
     },
     ...
   };

return methods;
})();

It will allow you to use internal vars as well as a side affect.

Alternatively,

add: function(params) { 
    var me = this;
    $.each(params["js"], function(key,value) { 
        // this.loadedcss error undefined
        me.loadedcss.push(value);
    });
}

Will also work

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

4 Comments

return methods at the end.. i didn`t find this any where in the books, any good link andrew ? thanks
@Andrew I think you have your syntax wrong - the method return pattern is normally used either with new (which isn't used by jQuery plugins) or as the return value of an immediately invoked function expression. You'd need to wrap it so: $.plugin = (function() { ... } )()
@user397916 Not really, I learnt it from reading the jQuery source. It's just really nice because it lets your plugin have private variables.
@user397916 An example can be found in the jQuery Plugin Guide
1

Alias the this parameter to something else (self in the example below) and then use that alias instead inside your callback function.

add: function(params) { 
    var self = this;
    $.each(params["css"], function(key,value) { 
        self.loadedcss.push(value);
    });
},

You can't use this inside the callback because $.each() will have set it to the current array element's value.

Comments

0

$.plugin is just a an object. It should be a function in order to create a new instance of it then you can use this in its methods. You can try this now

$.plugin = {

    loadedcss   : [],

    add: function(params) { 
        $.each(params["js"], function(key,value) { 
          // this.loadedcss error undefined
          $.plugin.loadedcss.push(value);
        });
    },

    end: function(){
        //console.log(this.loadedcss);
    }
};

6 Comments

JSON is a notation, not a data type.
@Alnitak - Yes it is not a data type.
@user397916 - With your edited code also you still cannot use this because it will point to current value params["js"] collection or map. Check my edited answer.
@ShankarSangoli my point was that there's no such thing as a 'json object' in Javascript. What he has here is an object literal, which happens to look like JSON.
@Alnitak - I totally agree with you :)
|

Your Answer

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