JavaScript - The Function() Constructors

The Function() Constructor

The function statement is not the only way to define a new function; you can define your function dynamically using Function() constructor along with the new operator. Constructor is a terminology from Object Oriented Programming. You may not feel comfortable for the first time, which is OK.

  •       
                        <script type = "text/javascript">
    var variablename = new Function(Arg1, Arg2..., "Function Body");
    </script>
                        

    Following is the syntax to create a function using Function( ) constructor along with the new operator.

    The Function() constructor expects any number of string arguments. The last argument is the body of the function – it can contain arbitrary JavaScript statements, separated from each other by semicolons.

    Notice that the Function() constructor is not passed any argument that specifies a name for the function it creates. The unnamed functions created with the Function() constructor are called anonymous functions

  •       
                        <html>
    <head>
    <script type = "text/javascript">
        function secondFunction() {
            alert('Hello, You Called?')
        }
    </script>
    
    </head>
    
    <body>
    <p>Click the following button to call the function</p>
    
    <form>
    <input type = "button" onclick = "secondFunction()" value = "Call Function">
    </form>
    
    <p>Use different parameters inside the function and then try...</p>
    </body>
    </html>
                        

    Dess App

    DessApp is an Integrated E-learning Education, Interactive and User-friendly features, smarter options and redefining your school costs effectively and efficiently.

    View
    1 1