I’m trying to set some properties inside an object. I’d love not having to tell the object what to expect from the outside (making the object more flexible, imo), so I tried this, without success :
slideshow = {
init : function(data) {
if (!data)
return false;
$.each(data,
function(index, value) {
// this isn't working, and don't know how to
this[index] = value;
}
);
alert(this.options.interval);
return true;
},
};
Think it’s pretty clear what’s missing.
Any ideas? Cheers!
update
var data = {
options : [
{
interval : 8000,
target : '#slider img',
},
],
images : [
{
img : 'assets/img/assets/img/slider_test_img.png',
title : 'Uniforme de Restauração 1',
description : 'blablabla descricao',
url : 'http://www.google.pt',
},
{
img : 'assets/img/assets/img/slider_test_img.png',
title : 'Uniforme de Restauração 2',
description : 'blablabla descricao 2',
url : 'http://www.google.pt',
},
{
img : 'assets/img/assets/img/slider_test_img.png',
title : 'Uniforme de Restauração 3',
description : 'blablabla descricao 3',
url : 'http://www.google.pt',
},
],
}
Either make a direct reference to the object:
or don’t use
$.each(), and just use afor-inloop for an object, or aforfor an array, since that won’t change thethisvalue:Or you could use the
jQuery.proxy()[docs] method to establish thethisvalue in the$.each()handler.