I watched Lee Brimelow’s Object-Oriented JavaScript tutorials : http://www.gotoandlearn.com/play.php?id=159
Now I have a question :
See following script :
(function (window){
function Car(a,b,c){
this.a=a;
this.b=b;
this.c=c;
}
window.Car=Car;
}(window))
My question is :
What if there is another “class” called Car created by other dev ? then will be conflicting ?
Thanks
FlashTang said
I watched Lee Brimelow’s Object-Oriented JavaScript tutorials : http://www.gotoandlearn.com/play.php?id=159
Now I have a question :
See following script :
(function (window){ function Car(a,b,c){ this.a=a; this.b=b; this.c=c; } window.Car=Car; }(window))My question is :
What if there is another “class” called Car created by other dev ? then will be conflicting ?
Thanks
This isn’t JS related at all, this is the core fundamentals of OOP . If you had an index file and some other developer had an index file of course there would be conflicts.
Solutions:
extending the class.
Adding to the existing class.
Scrapping yours and use his.
- Community Superstar
- Item was Featured
- Author was Featured
- Has been a member for 5-6 years
- Won a Competition
- Sold between 50 000 and 100 000 dollars
- Bought between 10 and 49 items
- Referred between 50 and 99 users
- Europe
window.Car=Car;
This line is the only link to that Car class… Otherwise is not visible outside the wrapping function. As long you don’t rewrite the window.Car with another thing… that Car class is safe. After rewrite you will lose the link to what is inside that wrap function.
- United States
- Has been a member for 4-5 years
- Exclusive Author
- Author was Featured
- Sold between 50 000 and 100 000 dollars
- Item was Featured
- Contributed a Tutorial to a Tuts+ Site
- Author had a Free File of the Month
You could use namespacing to help prevent conflicts
(function (window){
function Car(a,b,c){
this.a=a;
this.b=b;
this.c=c;
}
window.flashTang = {};
window.flashTang.Car = Car;
}(window))
alert(flashTang.Car);
Thanks Thecodingdude & wickedpixel ’s explain , and Jack’s solution , it works 

window.FlashTang=window.FlashTang || {};
If they are the same ?
(function (window){
}(window))
(function (window){
})(window)
