PhotoDune

Q : JavaScript OOP

1128 posts
  • Author had a File in an Envato Bundle
  • Has been a member for 4-5 years
  • Sold between 10 000 and 50 000 dollars
  • Exclusive Author
  • Referred between 10 and 49 users
  • Bought between 50 and 99 items
FlashTang says

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

624 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 100 and 1 000 dollars
Thecodingdude says

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.

2988 posts
  • Community Superstar
  • 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
+1 more
wickedpixel says

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.

5010 posts The Dude Abides
  • United States
  • Elite Author
  • Has been a member for 4-5 years
  • Exclusive Author
  • Sold between 50 000 and 100 000 dollars
  • Contributed a Tutorial to a Tuts+ Site
  • Author had a Free File of the Month
+4 more
CodingJack says

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);
1128 posts
  • Author had a File in an Envato Bundle
  • Has been a member for 4-5 years
  • Sold between 10 000 and 50 000 dollars
  • Exclusive Author
  • Referred between 10 and 49 users
  • Bought between 50 and 99 items
FlashTang says

Thanks Thecodingdude & wickedpixel ’s explain , and Jack’s solution , it works :)

1128 posts
  • Author had a File in an Envato Bundle
  • Has been a member for 4-5 years
  • Sold between 10 000 and 50 000 dollars
  • Exclusive Author
  • Referred between 10 and 49 users
  • Bought between 50 and 99 items
FlashTang says
Here is a safe pattern I want to share :D
window.FlashTang=window.FlashTang || {};  
If they are the same ?
(function (window){

}(window))
(function (window){

})(window)
by
by
by
by
by