+ 2
Javascript Object
Javascript is an OOP(Object Oriented Programming) Language, it uses objects to do everything.
What is an object?
An object is a collection of properties in key-value form. Key is the name of property and value is assigned to the property.
When a property is assigned to a function, it is called the object's method.
Suppose, you have a cat. Let's describe cat in js
var cat = {
name: "kitty" // property.
color: "brown" // again property.
makeSound: function(){ // it's method.
console.log("Mew, Mew!");
}
};
How to access methods and properties?
Properties are accessed by or objectName["propertyName"]; For example -
cat.name; cat.color;
cat["name"]; cat["color"];
Methods are also called like properties but we use parathensis after them. Like objectName.methodName() or objectName["methodName"]();
Generally, dot notation is used.
for example -
cat.makeSound();
cat["makeSound"]();
Types
There are two types of objects- Native and Host objects.
Native Objects are provided by javascript and will work in every environment. Ex- Date, Math, etc.
Host Objects are provided by environments in which javascript runs. These objects will not work in all environments. Ex- window, document, location, history, XMLHttpRequest, etc.