Functions and Prototypes in JavaScript

lassiecoder
3 min readApr 25, 2020

Functions

  • Particular business logic is called a function
  • Functions are used to reuse the business logic
  • As per year ES6 to ES11 we’ve following type of functions :
    (a) Named function
    (b) Anonymous / arrow / callback function
    (c) Rest parameters in function
    (d) Optional parameters in function
    (e) Default parameters in function
    (f) Constructor functions

Named function

  • The function with the name is called the named function

Anonymous function or Arrow function or callback function

  • The function without a name is called an anonymous function
  • It is also called Arrow function
  • Arrow function concept introduced in ES6
  • An arrow function is secured compared to the other functions
  • Arrow functions behave like callback functions

Rest parameters in function

  • “…” technically called as spread operator
  • Spread operator introduced in ES6
  • The spread operator has the capability to hold multiple values
  • Because of spread operator argument behaves like an array
  • The default value of spread operator augment is “[ ]”
NOTE: We can’t take more than one rest parameters.

Optional parameters in function

  • While calling the functions few parameters are optional
  • We’ll represent optional parameters by using “?”
  • This concept also introduced in ES6
  • Optional parameters will work in “Typescript environment”
  • Superset of JavaScript is called as “Typescript”

Environmental setup of Typescript

  • where “npm” stands for node packaging manager
  • “npm” is the inbuilt tool for node.js
  • “npm” helps to install Typescript
  • “-g” stands for global installation

Execution of Typescript

  • Typescript file should’ve the extension “.ts”
  • We will execute Typescript by using “tsc” tool
  • “tsc” stands for Typescript compiler
  • If we execute Typescript in current path equalent “JavaScript” file will be generated
  • Converting Typescript to equivalent JavaScript is called as transpilation

Default parameters in function

  • While defining the functions, we initialize parameters with default values
  • Default parameters also introduced in ES6

Constructor parameters in function

  • It helps to create the classes like structure in JavaScript
  • In the constructor function, all members (variables and functions) should start with “this” keyword
Parameterized constructor

Prototypes in JavaScript

  • The prototype is the predefined property in JavaScript
  • Prototype property helps to add the properties and functions to existed class dynamically

Prototype chaining (Inheritance)

  • Getting the properties from the parent class to child class called as prototype chaining
  • create() is the predefined function in object class helps to implement the prototype chaining in JavaScript
SINGLE INHERITANCE EXAMPLE
MULTIPLE INHERITANCE

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

lassiecoder
lassiecoder

Written by lassiecoder

I'm a software developer. My expertise spans JavaScript, React Native, TypeScript, ReactJS, Next.js, and MongoDB. https://bento.me/lassiecoder

No responses yet

Write a response