How to create new object in javascript


How to create new object in javascript?


<div id="colorShowContainer"></div><br>
<div id="colorShowContainer1"></div><br>
<div id="colorShowContainer2"></div>

        <script>
        var colors = {

          firstColor: "Red",
          secondColor : "Green",
          thirdColor : "Blue",
         
          completeColor : function() {
            return this.firstColor + " " + this.secondColor + " " + this.thirdColor;
          }
        };
        // How to access first trick
        document.getElementById("colorShowContainer").innerHTML = colors.firstColor + " " + colors.secondColor;

        // How to access second trick
        document.getElementById("colorShowContainer1").innerHTML = colors['firstColor'] + " " + colors['secondColor'];

        // How to access method from the object
        document.getElementById("colorShowContainer2").innerHTML = colors.completeColor();

        </script>

No comments:

Note: Only a member of this blog may post a comment.

Copyright Reserved to Anything Learn. Powered by Blogger.