Home > DeveloperSection > Forums > JavaScript Construct?
Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    January-29-2013 2:39 AM

 JavaScript JavaScript 
Ratings:


 1 Reply(s)
 1167  View(s)
Rate this:

Hi Everyone!

Can you tell me how to create a JavaScript Construct?

Thanks in advance



AVADHESH PATEL

Total Post:604

Points:4228
Posted on    January-29-2013 9:12 AM

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>


Don't want to miss updates? Please click the below button!

Follow MindStick