Computer Science
Javascript Inheritance
Javascript Inheritance is a way of creating new objects based on existing objects. It allows objects to inherit properties and methods from other objects, reducing the amount of code needed to create new objects. Inheritance is achieved through the use of prototype chains.
Written by Perlego with AI-assistance
Related key terms
1 of 5
5 Key excerpts on "Javascript Inheritance"
- No longer available |Learn more
- Andrea Chiarelli(Author)
- 2016(Publication Date)
- Packt Publishing(Publisher)
superclass . Apart from the naming, the inheritance concept is the same, although of course it does not seem suited to JavaScript.We can implement inheritance in JavaScript in various ways. Consider, for example, the following constructor of person objects: function Person() { this.name = ""; this.surname = ""; }In order to define a programmer as a person specialized in computer programming, we will add a new property describing its knowledge about a programming language: knownLanguage .A simple approach to create the programmer object that inherits properties from person is based on prototype. Here is a possible implementation: function Programmer() { this.knownLanguage = ""; } Programmer.prototype = new Person(); We will create a programmer with the following code: var programmer = new Programmer();We will obtain an object that has the properties of the person object (name and surname ) and the specific property of the programmer (knownLanguage ), that is, the programmer object inherits the person properties.This is a simple example to demonstrate that JavaScript supports the inheritance principle of Object-Oriented Programming at its basic level. Inheritance is a complex concept that has many facets and several variants in programming, many of them dependent on the used language.In Chapter 4 , Inheriting and Creating Mixins , we will explore more in depth how JavaScript supports inheritance, analyzing different approaches and more advanced topics such as overriding and multiple inheritance.In Object-Oriented Programming, polymorphism is understood in different ways, even if the basis is a common notion—the ability to handle multiple data types uniformly. Support of polymorphism brings benefits in programming that go toward the overall goal of OOP. Mainly, it reduces coupling in our application, and in some cases, allows to create more compact code.Polymorphism
- eBook - ePub
- Ian Millington(Author)
- 2019(Publication Date)
- CRC Press(Publisher)
class keyword, to make it more accessible for programmers of other languages, but classes are still implemented under the hood using prototypes.Prototypes differ from classes in that any object can inherit from any other object. In a class-based language objects come in two flavors: classes and instances. Instances can inherit only from classes, and classes have a limited form of inheritance from one another, usually called subclassing. This is much simpler in prototypical languages. There is only inheritance, and any object can inherit from any other object. The practical upshot of this is that we are not limited to the two level hierarchy of class and instance. In Section 5.4 , discussing behavior trees, I described the common need for three levels when defining and instantiating AI. This maps nicely into JavaScript. It has the effect of allowing developers to create one root object for a broad class of characters, then an object can inherit from that to configure the settings for a particular character type (this intermediate stage may be done by a level designer or technical artist), and then a final step can see an object instantiate the character type for an individual character in the level.Unlike the other languages in this section, JavaScript is single threaded. As a language for augmenting web pages, it was designed to be event driven: the JavaScript runtime monitors for particular events (such as user input, network activity, or scheduled timeouts); when an event occurs, the runtime calls code that has been registered as interested; this code then runs sequentially for as long as it needs; and when there is no more code to run, control returns to the runtime, which waits until another event occurs. Code is guaranteed not to be interrupted while running, and no other code will be run at the same time. This is perfect for simple scripts in a browser, but some things take much longer to complete. We don’t want the whole JavaScript process to grind to a halt waiting for a result. In a web browser, this might be querying for data across the network, which in some cases might be measured in seconds. JavaScript uses callbacks for this (in later versions of JavaScript, these are wrapped in easier to use structures such as promises or ‘async’, though the underlying behavior is the same). A callback uses the same event process. You register some code to be called when an action completes (when the results from the server is received, for example), then start the action. This allows many callbacks to be waiting for data at any time. It is perfect for many server applications, where the code needs to wait for data from the database, from other services, or for files to be read. - eBook - PDF
- Patrick Carey, Sasha Vodnik, Patrick Carey(Authors)
- 2021(Publication Date)
- Cengage Learning EMEA(Publisher)
Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. CHAPTER 1 INTRODUCTION TO JAVASCRIPT 10 Understanding JavaScript Objects Before you can use script elements to create a JavaScript program, you need to learn some basic terminology that is commonly used in JavaScript programming in particular, and in other programming languages in general. In addition to being an interpreted scripting language, JavaScript is considered an object-based programming language. An object is programming code and data that can be treated as an individual unit or component. For example, you might create a carLoan object that calculates the number of payments required to pay off a car loan. The carLoan object may also store information such as the principal loan amount and the interest rate. Individual statements used in a computer program are often grouped into logical units called procedures, which perform specific tasks. For example, a procedure may contain a group of statements that calculate the sales tax based on the sales total. The procedures associated with an object are called methods. A property is a piece of data, such as a color or a name, which is associated with an object. In the carLoan object example, the programming code that calculates the number of payments required to pay off the loan is a method. The principal loan amount and the interest rate are properties of the carLoan object. To incorporate an object and an associated method in JavaScript code, type the object’s name, followed by a period, followed by the method. - eBook - PDF
Web Programming
Building Internet Applications
- Chris Bates(Author)
- 2014(Publication Date)
- Wiley(Publisher)
JavaScript has one more concept left that you need to understand before diving into DHTML. JavaScript tries to be an object-oriented (OO) language. It is not actually a true OO language like Smalltalk or Eiffel, but it tries, the primitive objects that JavaScript does provide are very useful. Because the built-in functions all use these ideas, you will need to get a grasp of them before going any further, so next up we have JavaScript objects for beginners. 7.1.1 Objects – A Brief Introduction Object orientation is one of the most powerful concepts yet developed by computer scientists. Objects are widely applicable and object-based systems can be developed using many languages. Experienced programmers who are used to developing in traditional languages such as COBOL or FORTRAN, which have historically not supported objects, often find the OO paradigm confusing. Beginning programmers tend to be able to think in object terms right from the start. As you will see in a while, JavaScript objects are easy to understand and use, but the language does not support all of the features found in most object systems. Before I discuss JavaScript objects I will just spin rapidly through the general idea of objects. Objects An object is a thing. It can be anything that you like from some data through a set of methods (which is the name for functions in OO.) to an entire system. The reason that object-orientation is such a powerful idea is that, quite simply, it lets software designers and 7.1. Data and Objects in JavaScript 199 developers mimic the real world in their designs. This idea might be so obvious as to be not worth mentioning, after all software is usually meant to solve real-world problems. The benefit of OO is the tight linkage between the description of a problem and the ways in which it can be solved. Real objects are more than just data or processing: data often describe something but the thing being described also has the ability to act. - eBook - ePub
- Darren Jones(Author)
- 2017(Publication Date)
- SitePoint(Publisher)
index.html in your browser. Providing options that the player can choose from makes the game much easier to play by not requiring any typing:Multiple-choice options in the quizYou can see a live example on CodePen .Chapter Summary
- Object-oriented programming (OOP) is a way of programming that uses objects that encapsulate their own properties and methods.
- The main concepts of OOP are encapsulation, polymorphism and inheritance.
- Constructor functions can be used to create instances of objects.
- ES6 introduced class declarations that use the class keyword. These can be used in place of constructor functions.
- Inside a constructor function or class declaration, the keyword this refers to the object returned by the function.
- All instances of a class or constructor function inherit all the properties and methods of its prototype.
- The prototype is live, so new properties and methods can be added to existing instances.
- The prototype chain is used to find an available method. If an object lacks a method, JavaScript will check whether its prototype has the method. If not, it will check that function’s prototype until it finds the method or reaches the Object constructor function.
- Private properties and methods can be created by defining variables using const and defining a function inside a constructor function. These can be made public using getter and setter functions.
- Monkey-patching is the process of adding methods to built-in objects by augmenting their prototypes. This should be done with caution as it can cause unexpected behavior in the way built-in objects work.
- A mixin method can be used to add properties and methods from other objects without creating an inheritance chain.
- Methods can be chained together and called in sequence if they return a reference to this.
- Polymorphism allows objects to override shared methods with a more specific implementation.
- The value of this is not retained inside nested functions, which can cause errors. This can be worked around by using that = this , using the bind(this)
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.




