0

Can anyone tell me why this script is not working properly and how to fix it?

The issue is when using alert() to debug the stored values, the data come out as Spanish instead of English for buyersGuideData["English"].Language.

var enumLanguage = { English: 'English', Spanish: 'Spanish' }
var buyersGuideData = [];
var foo = { Language: '', WarrantyFlag: 'No' }

//Build list array of objects...
buyersGuideData[enumLanguage.English] = foo;
buyersGuideData[enumLanguage.English].Language = "English";
buyersGuideData[enumLanguage.Spanish] = foo;
buyersGuideData[enumLanguage.Spanish].Language = "Spanish";

alert(buyersGuideData[enumLanguage.English].Language);  //Output was displayed as "Spanish", not English...
alert(buyersGuideData[enumLanguage.Spanish].Language);  //Output was displayed as "Spanish"...

[Edit - New example] - Is that what you meant?

buyersGuideData[enumLanguage.English] = { Language: '', WarrantyFlag: 'No' }
buyersGuideData[enumLangauge.Spanish] = { Langauge: '', WarrantyFlag: 'No' }

buyersGuideData[enumLanguage.English].Language = "English";
buyersGuideData[enumLangauge.Spanish].Language = "Spanish";
fletchsod
  • 3,560
  • 7
  • 39
  • 65

3 Answers3

2

You can solve this problem simply by using a function as an object generator. Replace the foo object by a foo function like this

var foo = function () {
    return {
        Language: '',
        WarrantyFlag: 'No'
    };
}

And then change the way you assign the object

buyersGuideData[enumLanguage.English] = foo();
buyersGuideData[enumLanguage.English].Language = "English";
buyersGuideData[enumLanguage.Spanish] = foo();
buyersGuideData[enumLanguage.Spanish].Language = "Spanish";

Everytime foo is called a brand new object is instantiated. In your example you are using the same object, this is why is showing the unespected behaviour.

devconcept
  • 3,665
  • 1
  • 26
  • 40
  • Ooohhh! I like that. It didn't turned out the way I expect but it is good enough to use. Thanks man! I can't wait for ES6 to make it's way to all web browsers. – fletchsod May 05 '15 at 18:22
  • 1
    The advantage of this pattern is that if the object being created is too complex and has many properties your code will be a lot more clean than using simple object literal creation – devconcept May 05 '15 at 18:24
1

You're assigning the same object to two different locations, then modifying the object twice.

What you need is two different objects. Create it twice:

buyersGuideData[enumLanguage.English] = { ... }

You can use merging if you want to start with a template.

Community
  • 1
  • 1
tadman
  • 208,517
  • 23
  • 234
  • 262
  • How would that works? I thought if an object is assigned, it create a seperate copy. – fletchsod May 05 '15 at 17:52
  • It's pretty simple. You can't put the same object into two different places and expect it to behave in two different ways. The singular `Language` property is being manipulated. You need to create two objects, either by long-form, or by copying. – tadman May 05 '15 at 17:54
  • 1
    "I thought if an object is assigned, it create a seperate copy." nope. – xdumaine May 05 '15 at 17:58
  • Is that what you mean. See my edit part to original posting. Also, why should we writing repeating script if a new instance of variable can be assigned instead? – fletchsod May 05 '15 at 18:06
  • "New instance of a variable" means making a copy, and this must be done explicitly. `x = a` and `y = a` both refer to the same object, so any modifications to `x` affect `y`. – tadman May 05 '15 at 18:20
0

You have a syntax error :

alert(buyersGuideData[enumLanguage.English].Langauge);  //Output was displayed as "Spanish", not English...
alert(buyersGuideData[enumLanguage.Spanish].Langauge); 

Langauge is wrong, try Language

tadman
  • 208,517
  • 23
  • 234
  • 262
FOP
  • 962
  • 1
  • 10
  • 21