banner 728x90

Destruction and function arguments

In Mobile Web Development 16 views
banner 728x90

The JavaScript language has enjoyed many great new features in recent years, including arrow functions, the spreading operator and standard argumentation values. Even if your browser does not yet support suggested JavaScript syntax refills, you can use a tool like Babel in your Node.js app to take advantage of it today.

One of my favorite new (ish) JavaScript functions is object destruction. If you are not familiar with JavaScript destructuring, this actually provides a shorter syntax for extracting the value of a object key without the dot notation:

// A sample object
const myObject = ;

// Destructuring
const  = myObject;
// x is 1, y is 2

The basic syntax for destructuring is fairly simple, but the use of destructuring with function arguments can be a bit more difficult if those argument values ​​have to have default values. The following is a function with arguments with default values:

function myFunction (text = "", line = 0, truncate = 100)  0;
truncate = truncate 

Regardless of the language, if a function requires more than ~ 3 parameters, it is probably best to pass an object name options or config to that possible key / contains values; the equivalent would look like:

function myFunction (config) 

// Use
myFunction ()

What if you want to use destructuring in JavaScript function arguments? The next signature of the function would be:

function myFunction () 

To define default settings in the function configuration, use the following:

function myFunction ( = ) 

Setting a standard with = is important; without default the following example would give an error message:

TypeError: property & # 39; key & # 39; from & # 39; undefined & # 39; or & # 39; null & # 39; can not be destroyed

Destructures is a great language function but can lead to confusion and even to mistakes. Hopefully the basics in this guide can help you navigate with JavaScript destructuring with features!

banner 728x90
    Resetting of file changes with git
    Resetting of file changes with git
    There are many different philosophies when it
    DDoS protection by Incapsula (sponsored)
    DDoS protection by Incapsula (sponsored)
    DDoS security is an incredibly important protection
    Long e-mail discussions: Be Gone !!
    Long e-mail discussions: Be Gone !!
    We've all been working on those long
    Changes to permissions for AWS Mobile Hub
    Changes to permissions for AWS Mobile Hub
    Until recently, when you first created an

    Leave a reply "Destruction and function arguments"

    Must read×