Monday, April 9, 2018

ES6 Arrow Functions: Fat and Concise Syntax in JavaScript

Arrow functions were introduced with ES6 as a new syntax for writing JavaScript functions. They save developers time and simplify function scope. Surveys show they’re the most popular ES6 feature:

Arrow functions: Axel Rauschmayer survey on favorite ES6 features

Source: Axel Rauschmayer survey on favorite ES6 features

Arrow functions: Ponyfoo’s survey on the most commonly used ES6 features

Source: Ponyfoo’s survey on the most commonly used ES6 features

The good news is that many major modern browsers support the use of arrow functions.

This post will cover the details of arrow functions — how to use them, common syntaxes, common use cases, and gotchas/pitfalls.

What Are Arrow Functions?

Arrow functions – also called “fat arrow” functions, from CoffeeScript (a transcompiled language) — are a more concise syntax for writing function expressions. They utilize a new token, =>, that looks like a fat arrow. Arrow functions are anonymous and change the way this binds in functions.

Arrow functions make our code more concise, and simplify function scoping and the this keyword. They are one-line mini functions which work much like Lambdas in other languages like C# or Python. (See also lambdas in JavaScript). By using arrow functions, we avoid having to type the function keyword, return keyword (it’s implicit in arrow functions), and curly brackets.

Using Arrow Functions

There are a variety of syntaxes available in arrow functions, of which MDN has a thorough list. We’ll cover the common ones here to get you started. Let’s compare how ES5 code with function expressions can now be written in ES6 using arrow functions.

Basic Syntax with Multiple Parameters (from MDN)

// (param1, param2, paramN) => expression

// ES5
var multiplyES5 = function(x, y) {
  return x * y;
};

// ES6
const multiplyES6 = (x, y) => { return x * y };

Code Example at JSBin.

The arrow function example above allows a developer to accomplish the same result with fewer lines of code and approximately half the typing.

Curly brackets aren’t required if only one expression is present. The preceding example could also be written as:

const multiplyES6 = (x, y) => x * y;

Basic Syntax with One Parameter

Parentheses are optional when only one parameter is present

//ES5
var phraseSplitterEs5 = function phraseSplitter(phrase) {
  return phrase.split(' ');
};

//ES6
const phraseSplitterEs6 = phrase => phrase.split(" ");

console.log(phraseSplitterEs6("ES6 Awesomeness"));  // ["ES6", "Awesomeness"]

Code Example at JSBin.

No Parameters

Parentheses are required when no parameters are present.

//ES5
var docLogEs5 = function docLog() {
    console.log(document);
};

//ES6
var docLogEs6 = () => { console.log(document); };
docLogEs6(); // #document... <html> ….

Code Example at JSBin.

Object Literal Syntax

Arrow functions, like function expressions, can be used to return an object literal expression. The only caveat is that the body needs to be wrapped in parentheses, in order to distinguish between a block and an object (both of which use curly brackets).

//ES5
var setNameIdsEs5 = function setNameIds(id, name) {
  return {
    id: id,
    name: name
  };
};

// ES6
var setNameIdsEs6 = (id, name) => ({ id: id, name: name });

console.log(setNameIdsEs6 (4, "Kyle"));   // Object {id: 4, name: "Kyle"}

Code Example at JSBin.

Use Cases for Arrow Functions

Now that we’ve covered the basic syntaxes, let’s get into how arrow functions are used.

One common use case for arrow functions is array manipulation and the like. It’s common that you’ll need to map or reduce an array. Take this simple array of objects:

const smartPhones = [
  { name:'iphone', price:649 },
  { name:'Galaxy S6', price:576 },
  { name:'Galaxy Note 5', price:489 }
];

We could create an array of objects with just the names or prices by doing this in ES5:

// ES5
var prices = smartPhones.map(function(smartPhone) {
  return smartPhone.price;
});

console.log(prices); // [649, 576, 489]

An arrow function is more concise and easier to read:

// ES6
const prices = smartPhones.map(smartPhone => smartPhone.price);
console.log(prices); // [649, 576, 489]

Code Example at JSBin.

Here’s another example using the array filter method:

const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

// ES5
var divisibleByThrreeES5 = array.filter(function (v){
  return v % 3 === 0;
});

// ES6
const divisibleByThrreeES6 = array.filter(v => v % 3 === 0);

console.log(divisibleByThrreeES6); // [3, 6, 9, 12, 15]

Code Example at JSBin.

Continue reading %ES6 Arrow Functions: Fat and Concise Syntax in JavaScript%


by Kyle Pennell via SitePoint

TensorFlow.js : Machine Learning in Javascript

TensorFlow.js is an open-source hardware-accelerated JavaScript library for training and deploying machine learning models.

Use flexible and intuitive APIs to build models from scratch using the low-level JavaScript linear algebra library or the high-level layers API.Use TensorFlow.js model converters to run pre-existing TensorFlow models right in the browser.Retrain pre-existing ML models using sensor data connected to the browser, or other client-side data.

The post TensorFlow.js : Machine Learning in Javascript appeared first on Best jQuery.


by Admin via Best jQuery

Yo! #010 – Braille Font, Coke’s 3D Billboard, Webflow eCommerce, Win Nerdy Cards

This week in Yo! I cover Braille Fonts, you can win 1 of 3 Nerdy Cards, Coke’s 3D Billboard, Webflow eCommerce, Stack Overflow 2018 developer survey results, a new inspiration section, beats, freebies & more ✌


 


by Rob Hope @robhope via One Page Love

Project Management Considerations for Your WordPress Project

Lean, Agile, Waterfall; there are dozens of project management methodologies out there, and each one works to abstract your project into a common series of tasks and formulas. 

When it comes to software engineering, this can become complicated. For instance, it can cause issues between developers and managers whose organization styles differ. The manager needs that layer of abstraction to keep track of necessary metrics. The developer, however, can suffer from continual small task fatigue and feelings of being micromanaged.

Regardless of the programming language, framework, or libraries, none of them will perfectly fit into the variety of project management methodologies that exist. So how do we improve processes?

By categorizing the differences between tools. Let’s dig into the distinct features that comprise WordPress, and how they can impact the perspectives of managers and developers.

How to Adapt Your Project Management System to WordPress

To adapt our system, we first have to understand the nuances of WordPress. Of course, we don’t need to take every coding standard or functionality difference into account, but we do need to refer to significant sections that may make a difference. We’ll group these into three categories:

  • Challenges: Any piece that needs to be planned around when defining tasks, milestones, and implementations for the project.

  • Risks: Large issues that should be hedged against when possible. These are likely weaknesses in the framework that may push back development if they come to fruition.

  • Opportunities: Unique benefits in the framework that may provide additional features, make development more efficient, or in some way provide a competitive or internal advantage.

The difficulty with identifying these sections is that while they can mostly be learned through research and preparation, many are simply experienced during the attempt. In addition, defining them requires critical evaluation from both developers and managers, which may not always occur.

To adapt your current project management system to WordPress, let’s take a look at the unique Challenges, Risks, and Opportunities that are commonly faced.

Unique Challenges of Using WordPress

Every Content Management System by nature has its own set of downsides. With the involvement of different parties possessing different goals, compromises are bound to happen. Whether it’s users sacrificing customization or developers losing maintenance ease, something has to give. Here are some of the challenges using WordPress presents:

Using an Open-Source Base

Having an open-source base brings with it a bevy of pros and cons. As far as the challenges that are brought on by this, here are the most important:

Code-Base Maintenance

WordPress’s open-source base means that you’ll benefit from regular improvements to the system, but have very little control over those improvements. If a particular bug or feature change is an issue with your build, there is no guarantee of when it will be dealt with. Of course, you can always contribute to the base itself to speed things along, but with so many users, your addition may not be approved. After all, what you have in mind may not be the best solution for most users.

Dealing With Updates

To combat this, you can modify your own codebase or extend it as necessary, but this creates a new set of challenges. If you’ve created a workaround, you will need to be aware of changes to the central codebase that may alter or correct your solution in the future. If you’ve modified the codebase, you will need to be aware that updating WordPress core may alter the functionality that you’ve built, and plan accordingly.

Building Non-Generalist Sites

Because of the sheer number of websites that rely on WordPress, it’s likely that there will come a time when your site and the future of WordPress might be at odds. This becomes more true as your site moves away from what a typical WordPress site might look like.

To counteract this, try to work within WordPress’s constraints as much as possible, to minimize any issues that might arise from future updates. If while planning your project a large portion seems to be fighting the core rather than benefiting from it, consider using another CMS. Otherwise, you can also advise clients against updating WordPress after the project launches, though that brings with it a new set of challenges.

“Piecemeal” Development

The last major challenge to be aware of is the separation of components within WordPress. The divided structure of plugins, themes, and core can be a great tool for planning and hierarchy, but introduces additional third-party software.

Plugins and themes that are being used, but have not been created in-house, should receive an extra level of care. Take the time to do a proper discovery of these components to deal with possible complications.

Unique Risks of Using WordPress

Risks are a level beyond challenges, typically indicating issues that could be catastrophic to a project or whose solutions rest outside of development itself. Take a look at the two biggest that I’ve run into:

Security Issues

With code coming from multiple sources, it’s inevitable that sometimes a bug or exploit will come to light that might leave your project vulnerable. While these issues are typically fixed within days of exposure, the time in-between can be especially hazardous.

Because of the large number of sites using WordPress, exploits become well known quickly and can potentially be utilized en masse. Making sure that your project uses a variety of security measures can help to reduce the risk during those couple of days, but sometimes the only solution is to wait for a patch.

Inclusion of Third-Party Projects

Plugins are one of the most important features for many WordPress users. On the development side, however, plugins introduce unknown elements. Since they can be upgraded separately from the rest of the system (and potentially by your client), utilizing plugins as a key component in your project could be problematic later on.

Additionally, plugins need to be properly vetted before inclusion, otherwise you risk the potential of including dangerous code within your project.

Unique Benefits of Using WordPress

WordPress may have its own risks and challenges, but it has plenty of benefits as well. After all, it’s the most popular CMS on the web for a reason. Here are the pros to the cons above:

Using an Open-Source Base

We talked about the downsides of an open-source base, but there are many upsides as well. Using WordPress is free, and it boasts a wide range of documentation as well as extensive tutorials around the internet. This means that developers can quickly get up to speed on your project, and expanding your team’s knowledge during a project isn’t as arduous a task.

The other major benefit of the open-source base is the multitudes of people that work together to make it happen. A team of a handful of individuals could make something similar, but it’s unlikely to happen at the same pace and quality as WordPress.

Having many varied developers contributing to the code, paired with structured reviews, means that your projects are built on a solid, quality source. Having a large number of contributors also speeds along production, allowing features to be added quickly and patches to be issued in limited timeframes.

Robust Third-Party Solution Availability

WordPress boasts an extensive array of plugins, themes, and code snippets that can help streamline the production process. By utilizing these third-party solutions, you can quickly prototype—and even implement—entirely finished components into your project, offering additional features and efficiency.

Even if a plugin doesn’t quite do what you want, the most popular ones adhere to WordPress coding standards, making them easily adaptable to your needs.

Compartmentalized Design

A predefined and well-structured hierarchy and template system can help projects start off in an organized way. Instead of spending time deciding on engineering structures, WordPress allows efficient work within a well-established system. In addition, it’s suitable for most project management systems and allows for multiple pieces of the project to be developed simultaneously.

This compartmentalized design also makes it easy to determine where issues originate, and to maintain code throughout a project’s iterations.

Aligning Team Perspectives

Taking a Content Management System like WordPress and breaking it down into how managers and developers perceive it can streamline communication overall. Integrating these perspectives in your project management style should alleviate some anxiety with your developers. It gives them the benefit of the doubt, while adding some much-needed understanding to the team.

If you're looking for other utilities to help you build out your growing set of tools for WordPress or for code to study and become more well-versed in WordPress, don't forget to see what we have available in Envato Market.

Did I miss any key parts of WordPress that project managers should be aware of? Let me know in the comments!


by Kyle Speaker via Envato Tuts+ Code

Site Palette

Clean little One Pager for Site Palette – a Chrome extension that generates website color palettes in several formats including Sketch Templates and Adobe Swatches.

Full Review | Direct Link


by Rob Hope @robhope via One Page Love

30 Seconds of CSS

Long-scrolling One Pager hosting a curated collection of useful CSS snippets you can understand in 30 seconds or less.

Full Review | Direct Link


by Rob Hope @robhope via One Page Love

Stuurmen

We turn burning ambition into brand strategy and arm you with digital awesomeness. Kill off the average – mediocrity is way too boring.
by via Awwwards - Sites of the day