Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7531191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:06:35+00:00 2026-05-30T05:06:35+00:00

Im wondering which way of using OOP in Javascript ist best way to go

  • 0

Im wondering which way of using OOP in Javascript ist best way to go with.

There is this prototype thing and you have the function style way. But both have very bad ways to inherit a base class.

So I tried to build a way to make this possible without having to use prototype and such.

function Car(name) {
    this.Name = name;

    this.FullName = function () {
        return this.Name;
    }
}

function SpecialCar(name, variant) {
    //BaseClass.apply(this, PARAMS AS ARRAY);
    Car.apply( this, [name] );

    //new property
    this.Variant = variant;

    //override function
    this.FullName = function () {
        return this.Name + " " + this.Variant ;
    }
}

var audi = new Car("audi");
$("#result").append(audi.FullName() + "<br>");

var audia3 = new SpecialCar("audi", "a3");
$("#result").append(audia3.FullName()+ "<br>");

You can check the jsFiddle here: http://jsfiddle.net/vu9ZF/2/

Is this okay or is it just bad-practice?

Would be very nice to get some answers why other ways are better because I just dont get it.

EDIT: Thank you all for answering me so fast!
I was trying to find some way of OOP in javascript for myself, not answering on how to do oop in javascript.
I dont like the way of using prototype for that because im using c# most of my worktime and its a bit confusing to switch between “c#-like oop” and “prototype oop”.
But someone in the comments told me that avoiding prototype isnt a good idea, soo ill drop that.

Thanks stackoverflow your all awesome & saved me so much time =)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:06:37+00:00Added an answer on May 30, 2026 at 5:06 am

    This is how it’s done:

    function Car ( name ) {
        this.name = name;
    }
    
    Car.prototype.fullName = function () {
        return this.name;
    }
    
    function SpecialCar ( name, variant ) {
        Car.apply( this, arguments );
        this.variant = variant;
    }
    
    SpecialCar.prototype = Object.create( Car.prototype );
    
    SpecialCar.prototype.fullName = function () {
        return this.name + ' ' + this.variant;
    }; 
    

    (You need to shim Object.create for IE8)

    Live demo: http://jsfiddle.net/3Rehr/


    So, the methods should be assigned to the constructor’s prototype object, not to the instances themselves.

    Also, in order to pre-process the instance with the super-constructor, simply do this:

    Car.apply( this, arguments );
    

    so the this.base thing is not needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Websphere portal 6.0 and I'm wondering if there's a way in which
I have a multithreading program, and I am wondering which of the way using
I'm wondering if there's an easier way using CDT for this refactoring task. Not
I wondering if there's a way to start an application which was deployed using
I was wondering which is the way to make this simple (and maybe stupid)
I am wondering is there any way to execute following shell script, which waits
I was wondering about which way i should do the following. I am using
I was wondering which is the best way (the one suggested by Apple guidelines
I am wondering which way is more efficient using jQuery data to bind data
I'm wondering if there is a way to whitelist controller authentication using before_filter, also

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.