2 ways to use javascript object entries

Posted under javascript sample code On By mohammed.mutawe

javascript object entries

JavaScript is the object based language and you should know how to use it like this because it is very useful, you will have a skill how to add names and variables inside each object and how to modify this value.

So what are javascript object entries and how do we use them correctly and what are their benefits in website development?

 

What the object is?

The easiest way to explain the meaning of the object is to associate this definition with anything that has predefined properties and everyone knows them in this way. Flying at the top and another feature is the presence of a jet engine that makes it fly at the top and another feature is the speed of up to 600 km per second or more.

All these properties are the unique thing that made the plane with this name, and also in programming, an object is a variable with properties either static or variable that you can run and execute wherever you want and you can modify some values ​​on or you can affect other variables through it.

 

Benefits

There are many benefits that are indispensable while using objects, namely:

  • Arranging the code well.
  • functions abstracting.
  • Access sequence of values.

 

JavaScript object entries help you in arranging your code well, for example, there is no need to create many global variables and then start giving them values ​​in general because this will lead to common programming errors such as equating variables with values ​​that may not represent any connection with objects.

If we go back a bit to look at the concept of objects, we will find that they are variables that are defined by a particular class and carry all its attributes, for example in C++ and before there were classes, getting a structured object was just a matter of adding Structures and then sorting the values ​​inside to get a structure object It can be used directly and simply.

Then the so-called Classes were added, which is the infrastructure for objects, in which static and variable variables are added. This is for C++. As for JavaScript, things are really a bit easy, as the object is defined in an easy way.

The same is the case with the abstraction of functions. If we have an object of the type of plane, for example, and we want to add the number of seats inside, then the plane.seats object is called and the desired value is entered. As for the abstraction of the values ​​and their sequence inside the object, this is what JavaScript achieved when it allowed you to access all the properties of the object with different names.

 

Create an object

I hope you are known what javascript object entries are and why they are used so widely in all programming languages, but before we start adding an object there are a number of things to consider:

  • Naming method.
  • Commitment to letter sensitivity.
  • Avoiding reserved words.

There are several rules that must be adhered to before making javascript object entries and they are the same as those that apply to the way those variables and functions are named, which we explained in variables lesson.

As for the sensitivity of naming objects, the JavaScript language has a high sensitivity in naming. When we name Plane, we will stick to the Caps characters from the English language. If we call it plane, the object will not work at all. The reason for this is that you are dealing with a machine language that does not distinguish the names well, but it saves the syntax that a programmer usually use.

The same is true for the reserved words of the JavaScript language, which means that you cannot name an object with the name let, for example, or int, or many of the reserved words for this language.

Ways to add an object

Well, I know you were curious about using javascript object entries, but the previous lines had to be explained to beginners, and there are usually two ways you can add objects in JavaScript:

  • Constructor functions.
  • Object initializer.

 

Construction functions

This is a common and traditional way of constructing objects in JavaScript and it focuses on the traditional syntax in the language. To illustrate this, when we create a plane object, we add it directly to its name as a function of the JavaScript functions and within it we set the elements and child properties:

function plane() {
 //Code Properties here.
}

In the first stage, we define a traditional function, which is an object named plane, and we will add some properties to it, so that it becomes as follows:

function plane(type,seats,speed) {
 this.type=type;
 this.seats=seats;
 this.speed=speed;
}

You can say that all we have done is the definition of a function that contains three parameters, which is the type of aircraft first, then the number of seats, and then the maximum speed of the aircraft. This is a traditional function that you can review in a Javascript function lesson.

Well, we will complete this method in one line of code, which is the work of a variable, let its name be our_plane, and then assign the values ​​to it, which consist of the type of plane, then the type of engine and the number of seats, so that the code becomes as follows:

var our_plane = new plane("Airbus A318" , "180 seat" , "800 KM/h");

In this way, we have the ability to create an infinite number of objects for aircraft types, which may save us time and effort to do entries and define them each time. This is the first method of javascript object entries, easy and simple. If you find it difficult to enter data, you can design an HTML template and put some buttons that It will make the task easier for you while connecting to data bases.

 

Object Initializers

The second method is the fastest in javascript object entries, which enables you to enter the necessary values ​​through one line of code that is few words and high quality. This method is useful in reducing the number of codes to get better performance in some cases that require many and varied codes for different tasks and you can do this task by You will find an abbreviated form as follows:

our_object_name={property:value;}

Well, I think that things became a little easier after I got to know the Object Initializers method, there is no difference except in the input formula and syntax only, and all I will do now is replace the values ​​of this method with real values ​​from our previous example, and you can apply this code on your page to find the difference:

<script type="text/javascript">
our_plane_2= {type:"Airbus A318",seats:"180",speed:"800"}

document.write("I want a plane with "+our_plane_2.seats+" seats.<BR>");

</script>

 

References

 

Leave a comment

Your email address will not be published. Required fields are marked *