Tuesday, 24 April 2018

Understanding Javascript

 

Functional Programming

A very familiar term "Functional Programming". This is some programming paradigm that javascript falls into. A functional programming language is declarative, that means programming with expressions or declarations instead of statements. There are lot more to know about functional programming like pure functions, and it avoids changing state and mutable data etc., but lets get started with javascript first.

Data Structure and Data Types

Javascript is loosely typed or a dynamic language. That means a variable in javascript has no specific value type, i.e. any type of value can be assigned ro re-assigned to any variable. As it's dynanically typed, type checking are done at runtime.

e.g.
var x = 'foo'; // String
var x = 29;    // Number

Data types

    Primitives:
        Boolean
        Null
        Undefined
        Number
        String
        Symbol (new in ES6)
      
    and Object

Object


Except Object all other types are immutable. Objects are collections of properties having value of any type, including object. Usually objects consists of several variables and/or functions of related data and/or functionality.

Comparing to a real life object, a javascript object consists of properties and behavior.

var car = new Object(); // Object Declaration

Access to properties of an object with a dot-notation:
ObjectName.PropertyName

car.name = 'Creta';
car.company = 'Hyundai';

There are different styles for creating objects in javascript, like:

1) Object literals

var car = {
  name: 'Creta',
  company: 'Hyundai',
  f: function() {},
  g: function() {}
};
 


2) Constructor Functions


function car() {
  return {
    name: 'Creta',
    company: 'Hyundai',
    f: function() {},
    g: function() {}
  };
}

var o = new thing();

In javascript most things are objects like core javascript String and Array.

Javascript Constructors:

Javascript does's support classes, but it has constructors. Constructors are regular functions used with new operator to create objects.

[Source: https://css-tricks.com/understanding-javascript-constructors/]
1) Native Constructor: Available automatically in the execution environment at runtime (e.g. Array, Object, etc)
2) Custom Constructor: Defines properties and methods for your own type of object.


A prototype-based language


Javascript is classless prototype-based programming. It gives you delegation inheritance.

Prototype-based programming is a style of object-oriented programming in which behaviour reuse (known as inheritance) is performed via a process of reusing existing objects via delegation that serve as prototypes. [Wikipedia]

Prototype

comming soon.. stay tuned...



No comments:

Post a Comment

PHP 7 Performance And Security

PHP is widely used for custom software development. Usage statistics indicate that PHP accounts for over 80 percent of all websites, toppi...