Jsoppsnotes
Jsoppsnotes
Jsoppsnotes
object based/prototyping
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.
array.length
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.
Array.sort() array.push() array.pop()
Array Object
Seq random
Index base properties
[] {}
Example:
Car is an object: Person is an object:
-properties -properties
Car model: I20 > name: siva
Car colour : white > age: 50
Car no: 5579 > gen: male
-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.
Syn:
function Const-name() constructor developing
{
this.property1=value; initializing code
this.property2=value;
...
this.method-name = function(){
code
};
this.method-name = function(){
code
};
...
}
Object Syn:
refname = new Const-name(); constructor calling
refname = new Const-name(args);
</script>
</body>
</html>
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}
];
//object array
var movies=[ new Movie("Bharath","Mahesh","Siva"),
new Movie("ASVR","Ntr","Trivikram")
];
Literal Syn:
Object.prototype.new-property = value;
Object.prototype.new-method = function() { code };
//creating object
let p = new Product("Soap",2,42.50);
Inheritance
> the process of creating a new object based on another object prototype (exists) is called
"inheritance".
OR
> the process of creating a new constructor function based on another constructor function
(exists already) is called "inheritance".
> one object is deriving from an existing object is called inheritance.
> hence all the properties and methods of the 1st constructor (parent) is inherited into the
2nd constructor (child).
> sharing
> by calling 1st object's constructor from 2nd object's constructor function.
Syn:
function ConstructorP(parameters) //parent
{
properties
methods
}