How to create Own object in JavaScript

In this article I am going to explain about JavaScript Create own object.
  • 2251

 JavaScript create Own object

An object is an special types of data that contain properties and method. In JavaScript there are two types of object. First is Pre-defined object that provide by JavaScript, like string, Array, Date etc. And second is Own object that created by user with own type own definition. 

Create JavaScript Own type object

<html>

<head>

    <script type="text/javascript">

        function myFunction() {

            Emp = new Object();

            Emp.name = "Rahul";

            Emp.address = "New Delhi";

            Emp.salary = "100000";

 

            alert(Emp.name);

            alert(Emp.address);

            alert(Emp.salary);

        }

    </script>

</head>

<body>

    <button onclick="myFunction()">

        click</button>

</body>

</html>

 

Create a function that construct objects:

 

<html>

<head>

    <script type="text/javascript">

        function Employee(name, address, salary) {

            this.name = name;

            this.address = address;

            this.salary = salary;

 

            document.write("Name=", this.name, " Address=", this.address, " Salary=", this.salary);

        }

 

        function myFunction() {

            obj = new Employee("Rahul", "Delhi", "3333");

        }

 

    </script>

</head>

<body>

    <button onclick="myFunction()">

        click</button>

</body>

</html>

 

Output


own.gif 


Further Readings

You may also want to read these related articles :here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

 

© 2020 DotNetHeaven. All rights reserved.