Functions

 Functions:

    - Set of statements called a function.

    - Functions meant for writing common business logic.

    - Functions are used to reuse business logic.

    - We have two types of functions:

                1. Named Functions

                2. Anonymous Functions


Named Functions:

    - The Functions with the name called as Named Functions.

    - In general, we will create functions with the "function" keyword.


Syntax.

    //Function Defination

    function function_name( [parameters] ){

        //Business Logic

    }

    //Call the function.

     function_name()


Ex_1:

   Create the function @fun_one

    @fun_one should display the message on the web page.

    Call the @fun_one

SCRIPT CODE:

 <script>

    function fun_one() {

        document.write("<h1>Welcome to Functions....</h1>");

    }

    fun_one();

</script>

Ex_2:

    Create the function @fun_one

    Pass the three parameters to @fun_one.

    @fun_one should display the parameters on browser console.

<script>

    function fun_one(arg1,arg2,arg3) {

        console.log(arg1+"<==>"+arg2+"<==>"+arg3);

    }

    fun_one("AngularJS","NodeJS","MongoDB");

</script>

Ex_3:

    - Create the two function @fun_one , @fun_two

    - Call the @fun_one in @fun_two

    - @fun_one should display the "welcome" message on web page.

    - Call the @fun_two

<script>

    function fun_one() {

        document.write("<h1>Welcome....!</h1>");

    }


    function fun_two() {

        fun_one();

    }


    fun_two();

</script>

Ex_4:

    - Create the function @fun_one.

    - Display the @fun_one definition on the web page.

    - Call the @fun_one.

    - @fun_one should display the "Welcome...!" on a web page.

<script>

    function fun_one() {

        document.write("Welcome...!");

    }

    document.write(fun_one);

    fun_one();

</script>


Ex_5:

    create the two function @fun_one, @fun_two

    @fun_two should return the @fun_one definition

<script>

    function fun_one() {

        document.write("Welcome....!");

    }

    function fun_two() {

        return fun_one;

    }

    fun_two()();

</script>

Ex_6:

    Pass the function definitions as arguments to another function.

<script>

    function fun_one(arg1,arg2,arg3) {

        console.log( arg1()+"<==>"+arg2()+"<==>"+arg3() );

    }

    function fun_two() {

        return "AngularJS";

    }

    function fun_three() {

        return "NodeJS";

    }

    function fun_four() {

        return "MongoDB";

    }

    fun_one(fun_two,fun_three,fun_four);

</script>

Ex_7:

    Pass the function definitions as arguments to another function.

<script>

    function fun_one(arg1,arg2) {

        console.log(arg1()()+"<==>"+arg2()());

    }

    function fun_two() {

        return fun_three;

    }

    function fun_four() {

        return fun_five;

    }

    function fun_three() {

        return "Hello_1";

    }

    function fun_five() {

        return "Hello_2";

    }

    fun_one(fun_two,fun_four);

</script>

Anonymous Functions:

    - The Functions without a name called Anonymous Functions.

    - Anonymous Functions are more Secured Compared to Named Functions.

    - Anonymous Functions utilizes heap memory efficiently.

    - We can assign the variables to call the Anonymous Functions.


Syntax.

    //Function Declaration

    var var_name  = function(){

        //Business Logic

    }

    //Function Calling

     var_name();


JSON:

    - JSON Stands for JavaScript Object Notation.

    - We Can Transfer the Data Over the Network by using either XML or JSON format.

    - JSON is lightweight compared to XML.

Syntax:

    Objects ===>  { }

    Arrays  ===>  [ ]

    data    ===>  key & value

    - key & value separated by using ":"

    - each "key & value" pair separated by using ","


Classes in JavaScript:

    - Collection of variables and functions called as a class.

    - We can create the classes by using the "function" keyword.

    - In a class, all the members (variables & functions) start with the "this" keyword.


Ex_1:

    function class_one(){

        this.sub_one = "AngularJS";

        this.sub_two = "NodeJS";

        this.sub_three = "MongoDB"

    };


    var obj = new class_one();

    console.log(obj.sub_one+"<==>"+obj.sub_two+"<==>"+obj.sub_three);


Ex_2:

    function class_one(arg1,arg2,arg3){

        this.param1 = arg1;

        this.param2 = arg2;

        this.param3 = arg3;

        this.my_fun = function(){

            return this.param1+"..."+this.param2+"..."+this.param3;

        }

    }


    let obj = new class_one("Hello_1","Hello_2","Hello_3");

    console.log(obj.my_fun());


Ex_3:

    function class_one(){

        this.fun_one = function(){

            return new class_two();

        }

    }

    function class_two(){

        this.fun_one = function(){

            return{

                "login":login

            }

        }

    function login(){

            return "Welcome...!";

        }

    }

    var obj_one = new class_one();

    var obj_two = obj_one.fun_one();

    var json_obj = obj_two.fun_one();

    console.log(json_obj.login());


Ex_4:

    function class_one(arg1,arg2,arg3){

        this.fun_one = function(){

            return arg1+"..."+arg2+"..."+arg3;

        }

    }

    function class_two(){

        this.fun_one = function(){

            return "AngularJS";

        }

    }

    function class_three(){

        this.fun_one = function(){

            return "FireBase";

        }

    }

    function class_four(){

        this.fun_one = function(){

            return "ReadTime DataBase !";

        }

    }

    var obj = new class_one(new class_two().fun_one(),

                            new class_three().fun_one(),

                            new class_four().fun_one());

    console.log(obj.fun_one());












Comments

Popular posts from this blog

AngularJS Syllabus