Objects in Java Script

Object is the way of storing group of data in key-value pairs. Object is denoted by symbol { }.

here it is difficult to understand which value is first and last name, perhaps by guessing. What if array values are interchanged


    <script>

      var mydetails ={
        fName:"ramesh",
        lName :"babu",
        age:29,
        address:["ammerpeta","Hyd"],
        salery:function(){
            return '99999';
        },
        fulname:function(){
            return this.fName +""+ this.lName
        },
        living :{
            city : "Hyderabad",
            country : "India",
        }
      }

      console.log(mydetails);
      console.log(mydetails.address)
      console.log(mydetails.fName);

      console.log(mydetails.salery())
      console.log(mydetails.fulname())

      console.log(mydetails.living)

    //   document.write(mydetails.salery());

    </script>


What is a Function

A function, in simple terms, is a (named) group of code that can be invoked. In other words, a function is a way to group together some code, give this group a name, and later invoke the code using the given name.

     <script>

        function msg(fname, lname){
            document.write("Hellow"+ fname+ ""+ lname+"<br>");
        }

        msg("Code","Guruva");
        msg("ramesh","babu");

    </script>


JavaScript Classes

In programming, classes are used a "blue print" for modeling real-world items in code. In JavaScript, the syntax for creating a class could be as simple as:



    <script>

        class message{
            code(){
                console.log("this is code")
            }
            ramesh(){
                console.log("this is ramesh");
            }
        }

        var abc = new message;

        abc.code();
        abc.ramesh();
    
    </script>


object with 'Class Constructors'


Whilst I was creating a weather app with Javascript, I was confused about what a class constructor does. Therefore, I decided to research about it and help others to understand how to work with them. A class constructor is nothing more than a way to create an object in ES6 so is a new way but incredibly useful to create and build applications.


    <script>

        class mobiles{
            constructor(n,p,m){
                this.name = n;
                this.price =p,
                this.model=m
            }
        }

        var fmobile = new mobiles("nokia",999,"6gb");
        var smobile = new mobiles("vivo",999,"16gb");

        console.log(fmobile);
        console.log(smobile);

    </script>