Broad Network


Important Anonymous Expressions in ECMAScript 2015

Custom Objects in ECMAScript Part 5

ECMAScript 6

Foreword: In this part of the series I explain anonymous and arrow function basics and classes in ECMAScript.

By: Chrysanthus Date Published: 19 May 2016

Introduction

This is part 5 of my series, Custom Objects in ECMAScript. In this part of the series I explain anonymous and arrow function basics and classes in ECMAScript. You should have read the previous parts of the series before coming here, as this is a continuation.

Functions
The following is a function declaration:

        function fn (word1, word2)
            {
             str = word1 + " who laughs " + word2 + ", laughs best.";
             return str;
            }

It has the reserved word, function and the name of the function. Try the following code:

    <script type="text/ECMAScript">

        function fn (word1, word2)
            {
             str = word1 + " who laughs " + word2 + ", laughs best.";
             return str;
            }

        stri = fn('He','last');

     alert(stri);

    </script>

The following is a function expression (anonymous):

        function (word1, word2)
            {
             str = word1 + " who laughs " + word2 + ", laughs best.";
             return str;
            }

This expression (structure) can be assigned to a variable. Try the following code:

        fn = function (word1, word2)
         {
               str = word1 + " who laughs " + word2 + ", laughs best.";
               return str;
         }

        stri = fn('He','last');

       alert(stri);

The following is a function expression (anonymous) without the reserved word, function, but with parameters and the arrow punctuator:

        (word1, word2) =>
         {
               str = word1 + " who laughs " + word2 + ", laughs best.";
               return str;
         }

This expression (structure) can be assigned to a variable. Try the following code:

        fn = (word1, word2) =>
         {
               str = word1 + " who laughs " + word2 + ", laughs best.";
               return str;
         }

        stri = fn('He','last');

       alert(stri);

Classes
The following is a class declaration:

        class Cla
            {
             concatenate (word1, word2)
                 {
                     var str = word1 + " who laughs " + word2 + ", laughs best.";
                     return str;
                }
            }

It has the reserved word, class and the name of the class. Try the following code:

    <script type="text/ECMAScript">

        class Cla
            {
             concatenate (word1, word2)
                 {
                     var str = word1 + " who laughs " + word2 + ", laughs best.";
                     return str;
                }
            }

        obj = new Cla();
        ret = obj.concatenate('He','last');
        alert(ret);

    </script>

The following shows a class expression (structure) assigned to a variable:

        class Cla
         {
         concatenate (word1, word2)
                 {
                 var str = word1 + " who laughs " + word2 + ", laughs best.";
                 return str;
                }
         }

This expression (structure) can be assigned to a variable. Try the following code:

        Cla = class Cla
         {
         concatenate (word1, word2)
                 {
                 var str = word1 + " who laughs " + word2 + ", laughs best.";
                 return str;
                }
         }

        obj = new Cla();
        ret = obj.concatenate('He','last');
        alert(ret);

The following is a class expression (anonymous):

        class
         {
         concatenate (word1, word2)
                 {
                 var str = word1 + " who laughs " + word2 + ", laughs best.";
                 return str;
                }
         }

This expression (structure) can be assigned to a variable. Try the following code:

        Cla = class
         {
         concatenate (word1, word2)
                 {
                 var str = word1 + " who laughs " + word2 + ", laughs best.";
                 return str;
                }
         }

        obj = new Cla();
        ret = obj.concatenate('He','last');
        alert(ret);

That is it for this part of the series.

Chrys

Related Links

ECMAScript Basics
ECMAScript Operators
Expressions in ECMAScript
Statements in ECMAScript
Custom Objects in ECMAScript
Functions in ECMAScript
ECMAScript Date Object
The ECMAScript String Object
ECMAScript String Regular Expressions
ECMAScript Template Literal
The ECMAScript Array
ECMAScript Sets and Maps
ECMAScript Number
Scopes in ECMAScript
Mastering the ECMAScript (JavaScript) eval Function
Sending Email with ECMAScript
ECMAScript Insecurities and Prevention
Advanced Course
Advanced ECMAScript Regular Expressions
Promise in ECMAScript 2015
Generator in ECMAScript 2015
ECMAScript Module
More Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course

BACK

Comments

Become the Writer's Follower
Send the Writer a Message