Tuesday, 1 September 2015

How to Disable Cache in FireFox and Crome?

In Firefox

type in the address bar about:config
then press the button i'l be careful i promise
then type in the bar browser.cache.disk.enable
then double click on it, to make it false
do the same with browser.cache.memory.enable

In Crome

The Chrome DevTools can disable the cache.

Right-click and "Inspect Element" to open them. Now click Network in the toolbar. Then check the "Disable cache" checkbox at the top.

There is another option too.

  • Pull up the Chrome developer console by pressing F12 and then (with the console open)
  • Right click (or hold left click) on the reload button at the top of the browser and select "Empty Cache and Hard Reload."

Friday, 21 August 2015

Abstraction VS Encapsulation

There was a question in an interview what is different between abstraction and encapsulation.
I have given the common answers i.e

Abstraction allows us to represent complex real world in simplest manner. It is process of identifying the relevant qualities and behaviors an object should possess, in other word represent the necessary feature without representing the back ground details.

Encapsulation It is a process of hiding all the internal details of an object from the outside real world. The word Encapsulation, like Enclosing into the capsule. It restrict client from seeing its internal view where behavior of the abstraction is implemented

I think with above answer interviewer is convinced but he is saying if purpose of both is hiding then why there is need to use encapsulation.At that time not able to convince him

On google i found

Abstraction

Abstraction has to do with separating interface from implementation.

Abstraction is the process of actually hiding the implementation behind an interface. So we are just aware of the actual behavior but not how exactly the thing works out internally. The most common example could the scenario where put a key inside the lock and easily unlock it. So the interface here is the keyhole, while we are not aware of how the levers inside the lock co-ordinate among themselves to get the lock unlocked.

To be more clear, abstraction can be explained as the capability to use the same interface for different objects. Different implementations of the same interface can exist, while the details of every implementation are hidden by encapsulation.

Finally, the statement to answer all the confusions until now - The part that is hidden relates to encapsulation while the part that is exposed relates to abstraction.

Encapsulation

Encapsulation has to do with disallowing access to or knowledge of internal structures of an implementation.

Our code might be used by others and future up-gradations or bug fixes are liable. Encapsulation is something that makes sure that whatever code changes we do in our code doesn't break the code of others who are using it.

Encapsulation adds up to the maintainability, flexibility and extensibility of the code.

Encapsulation helps hide the implementation behind an interface.

Wednesday, 12 August 2015

Rest Parameters VS the Arguments Object Parameters

Arguments Object Parameters

Since JavaScript functions can be passed any number of parameters, it’s not always necessary to define each parameter specifically. Early on, JavaScript provided the arguments object as a way of inspecting all function parameters that were passed without necessarily defining each one individually.

Rest Parameters

The rest parameter syntax allows to represent an indefinite number of arguments as an array.

Arguments Object Parameter Example

 function pick(object) {
     let result = Object.create(null);

     for (let i = 1, len = arguments.length; i < len; i++) {
         result[arguments[i]] = object[arguments[i]];
     }

     return result;
 }

 let book = {
     title: "Understanding ECMAScript 6",
     author: "Nicholas C. Zakas",
     year: 2015
 };

 let bookData = pick(book, "author", "year");

 console.log(bookData.author);   // "Nicholas C. Zakas"
 console.log(bookData.year);     // 2015

Rest Parameters Example

 function fun1(...theArgs) {
   console.log(theArgs.length);
 }

 fun1();  // 0
 fun1(5); // 1
 fun1(5, 6, 7); // 3

Difference between rest parameters and the arguments object

  • rest parameters are only the ones that haven't been given a separate name, while the arguments object contains all arguments passed to the function;
  • the arguments object is not a real array, while rest parameters are Array instances, meaning methods like sort, map, forEach or pop can be applied on it directly;
  • the arguments object has additional functionality specific to itself (like the callee property).

Monday, 10 August 2015

Default Parameters

In ECMAScript 5 and earlier, you would likely use the following pattern to accomplish Default Parameters:

 function makeRequest(url, timeout, callback) {
     timeout = timeout || 2000;
     callback = callback || function() {};
     // the rest of the function
 }
 

In this example, both timeout and callback are optional .

ECMAScript 6 makes it easier to provide default values for parameters by providing initializations that are used when the parameter isn’t formally passed. For example:

 function makeRequest(url, timeout = 2000, callback = function() {}) {
     // the rest of the function
 }

 //-----------------------------------
 // uses default timeout and callback
 makeRequest("/foo");

 // uses default callback
 makeRequest("/foo", 500);

 // doesn't use defaults
 makeRequest("/foo", 500, function(body) {
     doSomething(body);
 });
 

It’s possible to specify default values for any arguments, including those that appear before arguments without default values.

 function makeRequest(url, timeout = 2000, callback) {

     // the rest of the function

 }

 //--------------------------
 // uses default timeout
 makeRequest("/foo", undefined, function(body) {
     doSomething(body);
 });

 // uses default timeout
 makeRequest("/foo");

 // doesn't use default timeout
 makeRequest("/foo", null, function(body) {
     doSomething(body);
 });
 

Good thing is default value need not be a primitive value.

 function getCallback() {
     return function() {
         // some code
     };
 }

 function makeRequest(url, timeout = 2000, callback = getCallback()) {
     // the rest of the function
 }
 

Saturday, 8 August 2015

Destructuring Assignment

Destructuring assignment allows you to assign the properties of an array or object to variables using syntax that looks similar to array or object literals.

JavaScript developers spend a lot of time pulling data out of objects and arrays. Like

 var options = {
        repeat: true,
        save: false
    };

 // later

 var localRepeat = options.repeat,
     localSave = options.save;

With destructuring you will

 var options = {
        repeat: true,
        save: false
    };

 var { repeat: localRepeat, save: localSave } = options;
 console.log(localRepeat);       // true
 console.log(localSave);         // false

Destructuring nested objects

 var options = {
        repeat: true,
        save: false,
        rules: {
            custom: 10,
        }
    };

 var { repeat, save, rules: { custom }} = options;

 console.log(repeat);        // true
 console.log(save);          // false
 console.log(custom);        // 10

Array Destructuring

 var colors = [ "red", "green", "blue" ];

 var [ firstColor, secondColor ] = colors;

 console.log(firstColor);        // "red"
 console.log(secondColor);       // "green"

Tuesday, 4 August 2015

Let V/S Const in Javascript

Constant Looks Like const MAX_ITEMS = 30; you can use conventions like ALL_CAPS to show that certain values should not be modified.

  • Variables declared using const are considered to be constants
  • Constants are also block-level declarations, similar to let. That means constants are destroyed once execution flows out of the block in which they were declared, and declarations are not hoisted to the top of the block.
  • Also similar to let, an error is thrown whenever a const declaration is made with an identifier for an already-defined variable in the same scope. It doesn’t matter if that variable was declared using var (for global or function scope) or let (for block scope). For example:
    var message = "Hello!";
    let age = 25;
    
    // Each of these would cause an error given the previous declarations
    const message = "Goodbye!";
    const age = 30;
        
  • The big difference between let and const is that attempting to assign to a previously defined constant will throw an error in both strict and non-strict modes:

Friday, 31 July 2015

Javascript - “let” keyword vs “var” keyword

The difference is scoping. var is scoped to the nearest function block (or global if outside a function block), and let is scoped to the nearest enclosing block (or global if outside any block), which can be smaller than a function block.

Global:

They are identical when used like this outside a function block.

let me = 'go'; //globally scoped
var i = 'able'; //globally scoped
  

Function:

They are identical when used like this in a function block.

function ingWithinEstablishedParameters() {
    let terOfRecommendation = 'awesome worker!'; //function block scoped
    var sityCheerleading = 'go!'; //function block scoped
};
  

Block

Here is the difference. let is only visible in the for() loop and var is visible to the whole function.

function allyIlliterate() {
    //tuce is *not* visible out here

    for( let tuce = 0; tuce < 5; tuce++ ) {
        //tuce is only visible in here (and in the for() parentheses)
    };

    //tuce is *not* visible out here
};

function byE40() {
    //nish *is* visible out here

    for( var nish = 0; nish < 5; nish++ ) {
        //nish is visible to the whole function
    };

    //nish *is* visible out here
};
  

Wednesday, 29 July 2015

How to get flags of RegEx in ES6

In ES5

it’s possible to get the text of the regular expression by using the source property, but to get the flag string requires parsing of toString(), such as:

 function getFlags(re) {
     var text = re.toString();
     return text.substring(text.lastIndexOf("/") + 1, text.length);
 }

 // toString() is "/ab/g"
 var re = /ab/g;

 console.log(getFlags(re));          // "g"
 

In ES6

 var re = /ab/g;

 console.log(re.source);     // "ab"
 console.log(re.flags);      // "g"
 

New Function in ES6

repeat()

console.log("x".repeat(3));         // "xxx"
console.log("hello".repeat(2));     // "hellohello"


Object.is()

When you want to compare two values, you’re probably used to using either the equals operator (==) or the identically equals operator (===). Many prefer to use the latter to avoid type coercion during the comparison. However, even the identically equals operator isn’t entirely accurate. For example, the values +0 and -0 are considered equal by === even though they are represented differently in the JavaScript engine. Also NaN === NaN returns false, which necessitates using isNaN() to detect NaN properly.

In many cases, Object.is() works the same as ===. The only differences are that +0 and -0 are considered not equivalent and NaN is considered equivalent to NaN. Here are some examples:

console.log(+0 == -0);              // true
console.log(+0 === -0);             // true
console.log(Object.is(+0, -0));     // false

console.log(NaN == NaN);            // false
console.log(NaN === NaN);           // false
console.log(Object.is(NaN, NaN));   // true

console.log(5 == 5);                // true
console.log(5 == "5");              // true
console.log(5 === 5);               // true
console.log(5 === "5");             // false
console.log(Object.is(5, 5));       // true
console.log(Object.is(5, "5"));     // false




How can I check if one string contains another substring?

In ES5

indexOf returns the position of the string in the other string. If not found, it will return -1:

    var s = "foo";
    alert(s.indexOf("oo") > -1);
 

In ES6 there are three new methods includes(), startsWith(), endsWith()

 var msg = "Hello world!";

 console.log(msg.startsWith("Hello"));       // true
 console.log(msg.endsWith("!"));             // true
 console.log(msg.includes("o"));             // true

 console.log(msg.startsWith("o", 4));        // true
 console.log(msg.endsWith("o", 8));          // true
 console.log(msg.includes("o", 8));          // false