Object-oriented programming - Learn web development | MDN 25.02.
26, 00:28
Student(name, year)
methods
introduceSelf()
canStudyArchery() { return [Link] > 1 }
JS
if ([Link]()) {
// allow the student into the class
}
That way, if we want to change the ru0es about studying archery, we on0y have to update the Student c0ass, and a00
the code using it wi00 sti00 work.
Fn many OOP 0anguages, we can prevent other code from accessing an object's interna0 state by marking some
properties as private . This wi00 generate an error if code outside the object tries to access them:
class Student : extends Person
properties
private year
constructor
Student(name, year)
methods
introduceSelf()
canStudyArchery() { return [Link] > 1 }
student = new Student('Weber', 1)
[Link] // error: 'year' is a private property of Student
Fn 0anguages that don't enforce access 0ike this, programmers use naming conventions, such as starting the name
with an underscore, to indicate that the property shou0d be considered private.
OOP and JavaScript
Fn this artic0e, we've described some of the basic features of c0ass-based object-oriented programming as
imp0emented in 0anguages 0ike Java and C++.
Fn the two previous artic0es, we 0ooked at a coup0e of core JavaScript features: constructors and prototypes. These
features certain0y have some re0ation to some of the OOP concepts described above.
[Link] Page 6 of 8
Object-oriented programming - Learn web development | MDN 25.02.26, 00:28
constructors in JavaScript provide us with something 0ike a c0ass definition, enab0ing us to define the "shape"
of an object, inc0uding any methods it contains, in a sing0e p0ace. But prototypes can be used here, too. For
examp0e, if a method is defined on a constructor's prototype property, then a00 objects created using that
constructor get that method via their prototype, and we don't need to define it in the constructor.
the prototype chain seems 0ike a natura0 way to imp0ement inheritance. For examp0e, if we can have a
Student object whose prototype is Person , then it can inherit name and override introduceSelf() .
But it's worth understanding the differences between these features and the "c0assica0" OOP concepts described
above. We'00 high0ight a coup0e of them here.
First, in c0ass-based OOP, c0asses and objects are two separate constructs, and objects are a0ways created as
instances of c0asses. A0so, there is a distinction between the feature used to define a c0ass (the c0ass syntax itse0f)
and the feature used to instantiate an object (a constructor). Fn JavaScript, we can and often do create objects
without any separate c0ass definition, either using a function or an object 0itera0. This can make working with objects
much more 0ightweight than it is in c0assica0 OOP.
Second, a0though a prototype chain 0ooks 0ike an inheritance hierarchy and behaves 0ike it in some ways, it's different
in others. When a subc0ass is instantiated, a sing0e object is created which combines properties defined in the
subc0ass with properties defined further up the hierarchy. With prototyping, each 0eve0 of the hierarchy is represented
by a separate object, and they are 0inked together via the __proto__ property. The prototype chain's behavior is
0ess 0ike inheritance and more 0ike de"egation. De0egation is a programming pattern where an object, when asked to
perform a task, can perform the task itse0f or ask another object (its de"egate) to perform the task on its beha0f. Fn
many ways, de0egation is a more f0exib0e way of combining objects than inheritance (for one thing, it's possib0e to
change or comp0ete0y rep0ace the de0egate at run time).
That said, constructors and prototypes can be used to imp0ement c0ass-based OOP patterns in JavaScript. But using
them direct0y to imp0ement features 0ike inheritance is tricky, so JavaScript provides extra features, 0ayered on top of
the prototype mode0, that map more direct0y to the concepts of c0ass-based OOP. These extra features are the
subject of the next artic0e.
Summary
This artic0e has described the basic features of c0ass-based object oriented programming, and brief0y 0ooked at how
JavaScript constructors and prototypes compare with these concepts.
Fn the next artic0e, we'00 0ook at the features JavaScript provides to support c0ass-based object-oriented
programming.
Previous Overview: Advanced JavaScript objects Next
[Link] Page 7 of 8
Object-oriented programming - Learn web development | MDN 25.02.26, 00:28
Your b0ueprint for a better internet.
[Link] Page 8 of 8