Skip to main content

Javascript ECMAScript6 New Feature


Extended Parameter Handling


Another nice set of features introduced in ES6 is the extended parameter handling. These provide a very nice set of idioms that bring us very close to languages like C# or Java.
Default parameter values and optional parameters
Default parameters allow your functions to have optional arguments without needing to check arguments.length or check for undefined.
Let’s take a look at what that might look like:
let greet = (msg = 'hello', name = 'world') => {
  console.log(msg,name);
}
greet();
greet('hey');

Looking at the preceding code, you can see that we are using the new fat arrow (=>) syntax as well as the new let keyword. Neither of those are necessary for this example, but I added it to give you more exposure to the new syntax.
Running the preceding code produces the following result:

hello world
hey world 

Rest parameter

Handling a function with a variable number of arguments is always tricky in JavaScript. Rest parameters (…), indicated by three consecutive dot characters, allow your functions to have a variable number of arguments without using the arguments object. The rest parameter is an instance of Array, so all array methods work. 

function f(x, ...y) {
  console.log(y);
  // y is an Array
  return x * y.length;
}
console.log(f(3, 'hello', true) === 6);

Running the preceding code produces the following result:

["hello", true]
true 

You can have as many regular arguments as you’d like, but the rest parameter must always be the last. This may be one of the new features that seems a little foreign and weird, but over time you will find that this is a very nice feature and will come in very handy.

Spread operator

The spread operator is like the reverse of rest parameters. It allows you to expand an array into multiple formal parameters.

function add(a, b) {
  return a + b;
}
let nums = [5, 4];

console.log(add(...nums));

Running the preceding code produces the following result:


Again, this may seem strange, but once you start using both of these you will find them useful.
Let’s change this slightly to have both a regular parameter and a spread operator in the same call:

function add(a, b) {
  return a + b;
}
let nums = [4];
console.log(add(5, ...nums));

As with the previous example, we get the same result:


Let’s look at one more example when creating array literals:

let a = [2, 3, 4];
let b = [1, ...a, 5];
console.log(b);

Running the preceding code produces the following output:

 [1, 2, 3, 4, 5]                    


As you can see, you can get very creative with the usage of the spread operator!

Comments

Popular posts from this blog

Claims Class For security in .NET4.5 with C#

What is Claim ? A claim in the world of authentication and authorization can be defined as a statement about an entity, typically a user. A claim can be very fine grained: Ram is an admin Ram’s email address is Ram@yahoo.com Ram lives in Mumbai Tom’s allowed to view sales figures between 2009 and 2012 Tom’s allowed to wipe out the universe Claims originate from a trusted entity other than the one that is being described. This means that it is not enough that Tom says that he is an administrator. If your company’s intranet runs on Windows then Tom will most likely figure in Active Directory/Email server/Sales authorisation system and it will be these systems that will hold these claims about Tom.  The idea is that if a trusted entity such as AD tells us things about Tom then we believe them a lot more than if Tom himself comes with the same claims . This external trusted entity is called an  Issuer . The KEY in the key-value pairs is called a  Type  and the VALUE in the

Paytm integration

How to integrate Paytm to asp.net site   You will need to create an Checksum on your server. Kindly refer the dll file for the attachment.  Usage of CheckSum API: ·         Add provided “paytm.dll” as a “Reference” in your project. ·         Import namespace “paytm” in your Class with statement “using paytm”. ·         Now Generate CheckSum API as well as Verify CheckSum API are available as follows: o   String CheckSum.generateCheckSum(String masterKey, Dictionary<String, String> parameters) o   Boolean CheckSum.verifyCheckSum(String masterKey, Dictionary<String, String> parameters, StringcheckSum) ·         For Generating CheckSum, use following snippet code: String masterKey = “merchantKey” ; Dictionary<String, String> parameters = new Dictionary<string, string>(); parameters.Add("CHANNEL_ID", "WEB"); parameters.Add("TXN_AMOUNT", "1"); String checkSum = CheckSum.generateCheckSum(masterKey, parameters); ·        

Design Patterns

                                                      Design pattern 1. Factory Design Pattern  In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface. Step 1 Create an interface IShape.cs public interface I Shape { void draw (); } Step 2 Create concrete classes implementing the same interface. Rectangle.cs public class Rectangle : I Shape { @Override public void draw () { C onsole.WriteLine ( "Inside Rectangle::draw() method." ); } } Square.cs public class Square : I Shape { @Override public void draw () { C onsole.WriteLine ( "Inside Square::draw() method." ); } } Circle.cs public class Circle : I Shape { @Override public void draw () { C onsole.WriteLine ( "Inside Circle::draw() method." ); } } Step 3 Create a Factory to gen