$.each() not saving values
It's not often I ask a question myself!
Please could someone sanity check my code before I report it as a bug. Is this a bug?
I'm trying to alter the values of a custom object's properties, from within a $.each() loop. The full demo code is on jsfiddle:-
http://jsfiddle.net/alano/96XDz/
- var obj = {
- testA: "please change this",
- testB: "and change this too"
- };
- function doReplace() {
- $.each(obj, function (index, value) {
- var newVal = value.replace("change", "done");
- this[index] = newVal;
- //obj[index] = newVal;
- //obj[index] = newVal.toString();
- //value = newVal;
- });
- $("#result").text("Result: " + obj.testA + " + " + obj.testB);
- }
According to my interpretation of the
jQuery Documentation for jQuery.each() , and the elaborations in the Wiki articles at bottom of page, each of my highlighted attempts to write to the target object being iterated should be saved and available after loop has completed.
I have tested the code and it fails on both the browsers I tried (Google Chrome 24 and IE9).
Thanks in advance,
Alan