Institute of Computer Technology
B. Tech Computer Science and Engineering
Sub: (2CSE410) FRONT END TECHNOLOGIES
Practical 8
Name: Purva Patel
Enrollment no: 23162121017
Batch: 41 (BDA)
Class: A
AIM: Understanding prototypal inheritance and its implementation
● prototype
● __proto__
Tools Used: Code editor (vs code) and web browser (Google Chrome).
Theory:
Prototypal inheritance in JavaScript is a mechanism that allows objects to inherit
properties and methods from other objects. It’s a key concept in JavaScript’s object-oriented
programming paradigm. Every object in JavaScript has an internal link to another object
called its “prototype” When trying to access a property or method of an object, JavaScript
first looks for it directly on the object itself. If it’s not found, it then searches the object’s
prototype, and if it’s still not found, it continues up the prototype chain until it reaches the
end (null).
Proto and prototype both are objects that help in whether creating an array, object, or
function and provide access to those specific methods or objects directly without taking
memory and even it will give access to its constructor and all the array methods like push,
pop, etc.
● Proto: It is an actual object that provides a way to inherit properties from JavaScript with
the help of an object which is created with new. Every object with behavior associated has
internal property [[prototype]].
Syntax: Object.__proto__ = value
● Prototype: It is a special object which means it holds shared attributes and behaviors of
instances. It is a way to inherit properties from javascript as it is available in every function
declaration.
Syntax: objectTypeName.prototype.SharedPropertyName=value;
Code:
● Create an object whose name is Animal. Create two more objects whose names are Dog and
Tiger. Inherit a few properties of Animal object in Dog and Tiger objects.
● Create a function whose name is sumOfArray. The function will give the sum of all numbers
present in an array. Add this function to the Array prototype. Check if it is working correctly
or not
● Create a function whose name is findCountOfVowels. The function will give the count of all
vowels present in a string. Add this function to the String prototype. Check if it is working
correctly or not Output: Objective Achieved: through this practical, the knowledge of object
and prototypal inheritance is understood. The difference between __proto__ and prototype is
understood. Also, the implementation of property inheritance is done.
Output: