My IDE, RubyMine, says this is “potentially incorrect”:
function update_top_up_prices_via_localStorage(){
var index = localStorage.getItem("volxs");
$('.product-inner').each(function(){
var product = this.getAttribute('data-product-miles');
var cpm = ((localStorage.getItem("cpm_by_volxs_"+product)).split(","))[index];
$(this).find('table tbody tr td').each(function(){
var top_up_miles = Number(this.getAttribute('data-topup-miles'));
var price = Number(top_up_miles * cpm * 1.06);
price = price.toFixed(2);
$(this).text('\u00A3'+price);
})
});
}
My IDE says on the var top_up_miles line, “potentially invalid use of this. This checks for javascript this to be the same in closure as in the outer context.
The js works fine in Chrome and IE9. It does not work in IE8.
Is this wrong? Is there a better way to write this?
RubyMine doesn’t know that
$().eachexecutes the passed function with a specifiedthiscontext. If you want to avoid that warning, useetc.
The reason your code doesn’t work with IE8 probably has nothing to do with that. For greater compatability however, use jQuery-wrapped functions instead of native API functions:
$(this).attrinstead ofthis.getAttribute(thanks @freakish)