JAVASCRIPT CONSTRUCT?

Ankita Pandey

Total Post:183

Points:1285
Posted by  Ankita Pandey
 1484  View(s)
Ratings:
Rate this:

Hi Everyone!

Can you tell me how to create a JavaScript Construct?

Thanks in advance

  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: JavaScript Construct?

    Hi Ankita Pandey! 

    Use below code for creating a java script counstruct:-

        <script type="text/javascript">
            var color = 'black';

            function Box() {
                // private property
                var color = '';

                // private constructor
                __construct = function () {
                    alert("Object Created.");
                    color = 'green';
                } ()

                // getter
                this.getColor = function () {
                    return color;
                }

                // setter
                this.setColor = function (data) {
                    color = data;
                }

            }

            var b = new Box();

            alert(b.getColor()); // should be green

            b.setColor('orange');

            alert(b.getColor()); // should be orange

            alert(color); // should be black
       
        </script>

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!