1

Because of some kind of language problems, I have to get the same name variable from two JS files like this.

JS1.js
SameName = new Object();
SameName = {}


JS2.js
SameName = new Object();
SameName = {}

How could I do this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
AmyWuGo
  • 2,315
  • 4
  • 22
  • 26
  • Are both JS files included in the same document? If so, one should overwrite the other. – emerson.marini Aug 23 '13 at 09:31
  • Your code sets the same variable to a (new) empty object 4 times, but your question gives me no clue about what you *want* to happen. What do you want to achieve? – Quentin Aug 23 '13 at 09:37
  • sorry, I am so newbie to JS, how could I put them into different documents? or Could I create new document and get the value. Thank you. – AmyWuGo Aug 23 '13 at 09:38
  • why not give them different names (the variables that is) – ianace Aug 23 '13 at 09:40
  • @Quentin for example: japanese.ja file and english.js file. have the variable words = new Object(); words = {"love":{"number":{000}}}; the part of "love" is the word in Japanese or English. I want to get the "love" part. – AmyWuGo Aug 23 '13 at 09:43
  • @ianace I could change the name very easily, but my customs who have the original data would prefer do nothing except click some button and get the result file. Thank you. – AmyWuGo Aug 23 '13 at 09:46
  • @AmyWuGo i think the answer magyar1984 is pointing to something workable :) – ianace Aug 23 '13 at 09:54

2 Answers2

3

I suggest going through these two links. Namespacing seems like a good alternative.

Different external .js files with same variable names

How do I declare a namespace in JavaScript?

Community
  • 1
  • 1
Aashray
  • 2,753
  • 16
  • 22
2

if it is possible, copy every object property from the first file to another object (not just assign, but copy) before you load second file

if it is not possible, you will get only the object which source file appears later in the document

magyar1984
  • 186
  • 4
  • Could I copy one object from one JS file to my var then import the second JS file, so I could use both later? – AmyWuGo Aug 23 '13 at 09:55
  • @AmyWuGo yes, if you only need the initial state of the object from any of the files, just copy the properties to another object and than load another file like - also take a look on Aashray answer if you can edit scripts you load – magyar1984 Aug 23 '13 at 10:19
  • Thank you very much! I think I could advise my customers to change name, but before that this maybe the best way to do. – AmyWuGo Aug 23 '13 at 10:23