Greeting All,
I hope that you are awesome guys. That blog is all about Introduce to JavaScript object where i am trying to explain to some fundamental and important topics that can be helps us to overcome our problems.
Introduction to JavaScript Object
First of all I want to tell you that as you know Object is the main various part of the JavaScript language. And I want to clarify that JavaScript is not similar to Java . Both are different things. JavaScript is an Object Oriented Programming (OOP) language. A programming language can be called object-oriented if it provides some basic capabilities like : −
- Class : – Class can be defined as a template or blue print that describe the behaviors or states that object of its type support. Class can be contain many object in a single class.
- Object :- Object is an instance of a class . Object have states and behavior for example a dog has states like name, color as well as behavior like barking, eating etc.
- Inheritance :- Inheritance can be define as a process where one or more object acquire the properties and functionality of another class.
- Polymorphism :- Poly means many and morphism means form. polymorphism means many forms. We can be define as like A polymorphism is an ability to takes an object in many forms.
- Encapsulation :- Encapsulation is nothing but just a Technique where end user don’t know how to implement in that class, users can have to permission what to store and what to reject and that data can be read only or write only or both. In the simplest way if I have to define of Encapsulation then I can say like Encapsulation is a mechanism to wrapping the date variable and code action on the data method as a single unit .
Way to Creating and accessing of Object in JavaScript
- first way of creating Object An Object is creating with the help of figure brackets like { …… } which has some Properties . Before creating the object with the help of figure brackets we have to know that what is the Properties . So I have been define below in simplest way to easy to understand :-
- properties :- A property is a key – value pair, where
key
can be define as a string andvalue
can be define anything.
- properties :- A property is a key – value pair, where
Example : –
1 2 3 4 5 6 7 8 9 10 11 |
// There is the code that is use to create object // and thats way to create object called Object Literals let emp = { } ; // Object literal syntax // An Simple Example let emp = { // an object name: "Ram", // by key "name" store value "Ram" salary: 25000 , // by key "salary" store value 25000 }; |
- Second way of creating Object
An Object is creating with the help of constructor also. A behavior of Constrcor is just like same as a function that creates and initialize of object. And JavaScript also provides a special constructor function named called like Object() when we use of object() at the time of build the object. The return value of the Object() constructor is assigned to a variable.
Example :-
1 2 3 4 5 6 7 8 9 10 |
var book =new Object(); let user=new User(); var book=new Array("Html", "JavaScript"); // example to create object using constructor var book = new Object(); // Create the object book.subject = "JavaScript"; // this is trying to initialize properties to the object book.page = 458; |
- Way to Accessing Object
First way to Accessing Object :-
Example :-
1 2 3 4 |
// That is an Example that how to accessing properties of any created object document.write("Employee name : "+emp['name']); document.write("Employee Salary : "+emp['salary']); |
Second way to Accessing Object
1 2 3 4 |
// That is an Example that how to accessing properties of any created object document.write("Employee name : "+emp.name); document.write("Employee Salary : "+emp.salary); |
Next time I will come back with more advance and important topics. Thanks to all.
Recent Comments