Giter Club home page Giter Club logo

es6-lesson's Introduction

ES6: Getters and Setters & Template Literals

Getters & Setters

Definition: Getters and setters are used for defining computed properties. A computed property is one that uses a function to get or set an object value.

Example from ES5

var person = {
  name: "Beyonce",
  get name() {
    return this.name;
  },
  set personName(val) {
    this.name = val;
  }
}

Then in the console:

person.name = "Mrs. Carter"
var bestArtistEver = person.name
console.log(bestArtistEver);

Examples using ES6

class Person {
  constructor(name) {
    this._hello = name;
  }
  get personName() {
    return this._hello;
  }
  set personName (newName) {
    this._name = newName;
  }
}

Then in the console:

let adele = new Person("Adele")
adele.personName
adele.personName = "A-Dawg"
adele.personName

Video

video

Template Literals

Definition: Template literals are string literals that can stretch across multiple lines and include interpolated expressions.

Template Strings use back-ticks rather than single or double quotes.

They help make the following easier:

  • String interpolation
  • String concatenation
  • Multiline strings
  • String substitution

Example from ES5

Multiline strings in ES5:

var teacher = "Andy \n" + "is on a higher level"

Example from ES6

Multiline strings in ES6:

var teacher = `Andy
is on a higher level`

Example from ES5

String concatenation:

var person = "Andy Kim";
console.log('my name is ' + person + '!');

Example from ES6

Better string concatenation:

var person = "Andy Kim";
console.log(`My name is ${person}!`);

Video

video

es6-lesson's People

Watchers

James Cloos avatar Rasheeda Alexander avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.