Showing posts with label java-script. Show all posts
Showing posts with label java-script. Show all posts

Monday, August 20, 2018

Prototype inheritance in javascript


Understanding prototype inheritance in Javascript

Object oriented programming (OOP) is commonly used programming paradigm which is based on "Object". And object contains two things,  data and methods. Some of most popular OOP languages are C++, Java, Python etc.

But here , we are talking about javascript , So, is Javascript a OOP language ?
YES , Javascript is object oriented programming language, But it is different from other OOP languages like C++, java or python. 

Object oriented programming is generally of two type : CLASS based and PROTOTYPE based.

While languages like C++, java, python are class based OOP languages,  JavaScript is based on prototype based OOP. And this is why,  JavaScript is different from other OOP languages. 

In class-based OOP, class is defined before hand and the object is instantiated based on the classe . If two objects Mango and Banana are instantiated from Fruit class, they are  fruits inherently and it is sure that you may treat them in the same way; e.g. you can expect the existence of the same behavior such as color, weight or sweetness etc. 

In prototype-based OOP,  the object is the primary entity. No class exists . The prototype of object is another object to which the object is linked. Every object contains one prototype link  and there can be only one. Any new object can be created based on already existing objects chosen as their prototype. 

We can call two different objects Mango and Banana a fruit, if the object fruit exists, and both Mango and Banana have fruit as their prototype. The concept of the fruit class doesn't exist separately , but as the equivalence class of the objects sharing the same prototype. 

The data and methods of the prototype are delegated to all the objects of the equivalence class defined by it. The data and methods owned individually by the object may not be shared by other objects of the same class; e.g. the attributes sweetness may be unexpectedly absent in Mango . Only single inheritance can be done using the prototype.

Prototype based object oriented programming uses generalized objects, these objects can be cloned and extended.

// Example of true prototypal inheritance style 
// in JavaScript.
var foo = {name: "foo", one: 1, two: 2};

var bar = {two: "two", three: 3};
Object.setPrototypeOf(bar, foo); // foo is now the prototype of bar.

bar.one // Resolves to 1.
bar.three // Resolves to 3.
bar.two; // Resolves to "two"
bar.name; // unaffected, resolves to "foo"
foo.name; // Resolves to "foo"






Friday, August 17, 2018

Functional programming


What is Functional Programming ?


Functional programming is a way of programming in which we bind everything in pure mathematical functions style. It follows declarative type of programming. It mainly focus on 
“what to solve” while imperative style main focus is “how to solve”. 
It is mainly based on expressions instead of statements. An expression is evaluated to produce a value whereas a statement is executed to assign variables.

functional  programming Languages example: JavaScript, Haskell, Scala, Erlang, Lisp,OCaml, Common Lisp, Racket, ML, Clojure etc. 

Concepts


Functions are First-Class and can be Higher-Order

First class functions are represented as first class variable. The first class variables can be used in functions as parameter, can be returned from functions or stored in data structures. Higher order functions are the functions that take other functions as arguments and they can also return functions.

Pure functions

Pure functions have properties like .
1. it always produce the same output for same arguments .
2. it have no side-effects i.e. it don't modify any argument or global variables or output something.
3. it also make it easier to write parallel/concurrent applications.

Recursion

“for” or “while” loop are used in functional languages. Iteration is implemented through recursion in functional languages. Recursive function repeatedly call itself, until it reaches the base case.
eg: fib(n)
    if (n <= 1)
        return 1;
    else
        return fib(n - 1) + fib(n - 2);

Variables are Immutable

We can’t modify a variable after initialization in functional programming. We create new variables, we don’t modify existing variables, and it helps to maintain state in the runtime of  program. Once we declare a variable and assign its value, we can be sure that the value of that variable will never alter. 

Referential transparency

it does not have assignment statements, it means, the value of a variable in a functional program will not change after it is defined. it eliminates any chance of side effects as any variable can be replaced with its actual value at any point of execution. So, we can say functional programs are referentially transparent.

Wednesday, August 8, 2018

Remove a given element from an array in JavaScript?


Remove a given element from an array in JavaScript?

First of all we need to find the index of the given element in the array. we can find the index of element using array.indexOf() method.  it returns the first occurrence in the array or it returns -1 , if element is not present in the array. 
  for example , var list = [2,4,6,8,10]
                         var index = list.indexOf(4)   // returns 1

So, now we know the index of given number. 
After,  we will use array.splice() method to remove the element. 
Splice method can be used to add (or remove) element to (or from )
array, and it returns the removed elements. 


if the index is not -1, it means element is present in array.
Using splice method, we will delete remove the number.
   if(index>-1){
   list.splice(index,1)   // 1 represent number of elements to delete


References:


Thursday, July 5, 2018

Check whether a given string contains a sub-string using JavaScript ?


Check whether a given string contains a sub-string using JavaScript ?


There can be many ways to solve this problem. Some of the possible ways are given below. 


For ES6: includes .
The includes() method determines whether one string may be found within another string, returning true or false as appropriate.                                                          
Syntax : str.includes(searchString[, position])                                   Example: 
'Technical knowledge point'.includes('javascript'); // returns false      'Technical knowledge point'.includes('knowledge'); // returns true


For ES5 or below: indexOf . 
The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.                  Syntax: str.indexOf(searchString[, fromIndex])                                                                              
Example:  
'Technical knowledge point'.indexOf('javascript');   // returns -1        'Technical knowledge point'.indexOf('knowledge');    //returns 10               

Search
The search() method searches a string for a specified value, and returns the position of the match. The search value can be string or a regular expression. This method returns -1 if no match is found.
Syntax:  str.search(searchString)
Example: 
'Technical knowledge point'.search('javascript');   // returns -1
'Technical knowledge point'.search('knowledge');    //returns 10   
'Technical knowledge point'.search(/T/i); // returns 0
'Technical knowledge point'.search(/k/i);    //returns 10
'Technical knowledge point'.search(/p/i);    //returns 20
                                                                                                                            

How do I redirect to another web page using javascript ?


How do I redirect to another web page using javascript ?



There is three methods to redirect user to other web page using java-script.


  • window.location.href
  • window.location.replace
  • window.location.assign

window.location.href method is used to get location of current web-page. Also using this method we can redirect browser to another page.
Example:  window.location.href="http://techpowerinfo.blogspot.com/";


window.location.replace method removes the URL of current web page from the history. So, it is impossible to  go back to the previous (or original) web-page.
Example:  window.location.replace("http://techpowerinfo.blogspot.com/");


window.location.assign  method adds the URL of new web page to the history stack. So, it is possible to move back and forth , and redirects to new web page.
Example:  window.location.assign("http://techpowerinfo.blogspot.com/");