OOPs JS
OOPs JS
Ex: etc…
Object is an instance of a class, nothing but memory block (one copy of class)
“Object” is a predefine class; every class/object should be derived from “Object” class prototype.
Array Object
Sequence random
Index base properties (key)
[] { }
Example:
-methods -methods
Start() > sleep()
Change gear() > eat()
Stop() > talk()
In the above example the “car” object has three properties called “car model, car colour, car no”,
which have respective values.
“Object” is a predefine class; every class/object should be derived from “Object” class prototype.
Creating objects:
Object literals are represented as curly braces { }, which can include properties and methods.
The property and values are separated with:symbol
The method-name and body are separated with :symbol
Syntax:
const refname = {
: :
“property” value, property value, ...,
how to access?
refname.property
refname.property=value
refname.method-name()
Note:
Object is one of predefined class.
Every js class and every js object should be derived from an Object class
prototype.
document.write("Id :"+stu1.id+"<br>");
document.write("Name :"+ stu1.name+"<br>");
document.write("Marks:"+ stu1.marks+"<br>");
document.write(stu1+"<br><br>");
This keyword
The “this” keyword represents/substitutes the current working object . For example, if it is called for
the first time, the “this” key word represents the first object; if it is called second time, it represents the
second object.
"this" is a keyword
"this" is a predefined reference variable (Same as to stud )
"this" maintains reference of current working object.
"Stud" what it maintains "this" also maintains the same thing
"this" used within the object, but "stud" used outside the object
If you want access members within the object use "this".
If you want access members outside the object use "stud"
Note: If we want access properties inside method or constructor
function, we should use “this” keyword.
Constructor function
document.write(emps);
</script>
</body>
</html>
<!--exmaple on object arrays -->
<html>
<head>
<script>
function totalValue(prods) //user define function
{
let inventory_value = 0;
for(let i=0; i<prods.length; i+=1) {
amt= prods[i].inventory * prods[i].unit_price;
inventory_value +=amt;
document.write(prods[i].name, amt, "<br>");
}
return inventory_value;
}
</script>
</head>
<body>
<script>
let products = [{ name: "chair", inventory: 5, unit_price: 45},
{ name: "table", inventory: 10, unit_price: 120},
{ name: "sofa", inventory: 2, unit_price: 500}
];
prototype
the "prototype" generally represents model of the object
(structure), which contains list of properties and methods of the
object.
"prototype" is a predefine property.
All JavaScript objects inherit properties and methods from a prototype:
Student objects inherit from Student.prototype
Date objects inherit from Date.prototype
Array objects inherit from Array.prototype
Literal Syn:
Object.prototype.new-property = value;
Object.prototype.new-method = function() { code };
refname.new-prop=value
refname.new-method=body
Functional Inheritance
> The process of creating a new object based on another object
(exists) is called "Functional inheritance".
OR
> The process of creating a new constructor based on another
constructor (exists) is called "Functional inheritance".
> Hence all the properties and methods of the 1st constructor
(parent) is inherited into the 2nd constructor (child).
> sharing
1 constructor parent/base
Prop
Methods
Inheritance
> the process of creating a new class based on another class (exists) is called "inheritance".
> used to build the relationship between multiple classes
> used to extending functionalities of existing classes without modifying source code.
> hence all the properties and methods of the 1st class (parent) is inherited into the 2nd class
(child).
> sharing
> by calling 1st object's constructor from 2nd object's constructor function.
Syn:
class ClassName1 //parent
{
constructor(parameters)
{
Properties init
}
methods
}
super()
super() is a predefine function, it's used to call parent class constructor from child class
constructor to build inheritance.