Why use OOP class style inheritance in javascript?

If I'm not completely mistaken, every javascript framework / library / approach today tends to mimic OOP class style inheritance. The reasons for this, it seems, are people who think about the inheritance of the OOP class, it is much easier to understand and that most programmers know OOP.

In my experience, I have not found evidence for any of these opinions. I think the prototypical inheritance of javascript is just fine (and I doubt it is useful to force a different paradigm in the language than the one on which it is built). Most of the developers I meet are not even good at classic OOP. So, what are the reasons for choosing classic OOP style inheritance over prototype inheritance?

+5
source share
8 answers

I think the answer in your question is that most programmers are much more familiar with the OOP class than with the prototype.

In fact, I have come to the point that most do not believe that you can have objects without classes.

+6
source

Note that even if you argue about the prototype of OOP, you call it “prototype”, and OOP based on classes is “OOP”. therefore, you yourself suffer from this bias, thinking OOP => classes, prototypes => something else.

and since most people think that OOP is the right way, regardless of the problem, prototypes should be lower.

, , :

  • : "OOP = > classes"
    • : " , "

, , . :

  • , - . , , , .

    • , .
+6

, , ,

.

, . , , Java. , , " ", , . , , , , .

, , . javascript - OOP . , OOP , javascript .

, javascript, . .

+4

, . , , , .

+3

, ? , , " " . , , . , YAHOO.lang.extend.

. , ... - :

// Prototypal Inheritance
Object.prototype.inherit = function(p) {
    NewObj = function(){};
    NewObj.prototype = p;
    return new NewObj(); 
};

// Paraphrasing of Nicholas Zakas Prototype Inheritance helper
function inheritPrototype(subType, superType) {
    var prototype = Object.inherit(superType.prototype);
    prototype.constructor = subType;
    subType.prototype = prototype;
};
function SubType(name, age) {
    Parent.call(this, name);
    this.age = age;    
};
inheritPrototype(SubType, Parent);  
SubType.prototype.getAge = function() {
    return this.age;
};

:

   describe 'Parisitic Combination Inheritance'
 it 'should use inheritPrototype (to call parent constructor once) and still work as expected'
     sub = new SubType("Nicholas Zakas", 29)
     sub.toString().should.match /.*Nicholas Zakas/
     sub.getAge().should.eql 29
     charlie = new SubType("Charlie Brown", 69)
     charlie.arr.should.eql([1,2,3])
     charlie.arr.push(999)
     charlie.arr.should.eql([1,2,3,999])
     sub.arr.should.eql([1,2,3]) 
     sub.should.be_an_instance_of SubType
     charlie.should.be_an_instance_of SubType
     (sub instanceof SubType).should.eql true 
     (sub instanceof Parent).should.eql true 
 end
    end

, , , : , , ;) : instanceof works ( , , ); , (biggie!); , (biggie!).

, : JD TDD

+3

, "" , "" . - - , clone ala SELF.

+1

, . , JavaScript. , .

- , .

0
source

One of the reasons may be that your server side will most likely be OO (ASP.NET, Java, etc.), so it’s just easier to think in the same paradigm on the client. Not necessarily better, but easier.

0
source

All Articles