Using Functions as Methods - Javascript Language Basics

Javascript examples for Language Basics:Introduction

Introduction

You can add functions to an object too.

Demo Code

ResultView the demo in separate window

      <!DOCTYPE HTML>
      <html>
          <head>
              <title>Example</title>
          </head>
          <body>
              <script type="text/javascript">
                  var myData = {//  w  w w  . j  a v a2 s. c om
                      name: "java2s.com",
                      topic: "CSS",
                      printMessages: function() {
                          document.writeln("Hello " + this.name + ". ");
                          document.writeln("Today is " + this.topic + ".");
                      }
            };

            myData.printMessages();

        </script>
    </body>
</html>

Related Tutorials