OOPs JS
OOPs JS
OOPs JS
Ex: etc…
Object is an instance of a class, nothing but memory block (one copy of class)
Properties: details about the object. Properties are the variables which are stored inside the
Object. Properties are used to store data about specific person,product or thing.
Ex: array.length=5
Methods: to perform manipulations on the properties. Methods are the functions stored
inside the object. Methods read values from properties, write values into properties, to
perform logical operations.
Ex: array.sort() array.push() array.pop()
Array Object
Seq random
Index base properties
[] {}
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
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:
how to access?
refname.property
refname.property=value
refname.method-name()
Note: every class and every object should be derived from a class called “Object” class(lib class).
document.write("Id :"+stu1.id+"<br>");
document.write("Name :"+ stu1.name+"<br>");
document.write("Marks:"+ stu1.marks+"<br>");
document.write(stu1+"<br><br>");
Note: 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.
If we want access properties inside method or constructor function, we should use “this” keyword.
Constructor function
Constructor is a function that receives an empty (created by new keyword) object, initializes
properties and methods to the object.
Constructor functions technically are regular functions. There are three conventions though:
1. They are named with capital letter first.
2. They should be executed only with "new" keyword, while object creation.
3. constructor functions don’t return any value, hence no return statement.
Object Syn:
refname = new Const-name(); constructor calling
refname = new Const-name(args);
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}
];
Literal Syn:
Object.prototype.new-property = value;
Object.prototype.new-method = function() { code };
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.
1 constructor parent/base
Prop
Methods
super()
super() is a predefine function, it's used to call parent constructor from child constructor to
build inheritance.