// old way
[1, 2, 3].map(function (number) {
return number * 2;
});
// [2, 4, 6]
// ES6 way
[1, 2, 3].map(number => number * 2);
// [2, 4, 6]
// old way
for (var i = 0; i < 2; i++) {
// mach etwas
}
console.log(i); // 1
// ES6 way
for (let i = 0; i < 2; i++) {
// mach etwas
}
console.log(i); // i is not defined
let lastName = "Muster";
let obj = {
firstName: "Hans",
lastName,
hobbies: ["Tennis", "Lesen"]
};
let obj = {};
obj.firstName = "Hans";
obj.lastName = "Muster";
obj.hobbies = ["Tennis", "Lesen"];
let name = 'Hans';
let templString = `Hallo
${name},
wie geht es dir?`
console.log(templString); // Hallo
// Hans,
// wie geht es dir?
function info(name, ...hobbies) {
hobbies.forEach(hobby => console.log(hobby));
}
info("Hans", "Tennis", "Squash", "Schach");
// "Tennis" "Squash" "Schach"
function multiply(multi, ...numbers) {
return numbers.map(num => multi * num);
}
console.log(multiply(2, 3, 4, 5, 6));
// [6, 8 , 10, 12]
// Arrays kopieren
let arr = [1, 2]
let newArr = [...arr]
// Arrays zusammenführen
let devs = ["Hans", "Reto"];
let testers = ["Peter", "Nicole"];
let employees = ["El jefe", ...devs, ...testers];
console.log(employees);
// ["El jefe", "Hans", "Reto", "Peter", "Nicole"]
let today = [2017, 2, 29];
console.log(new Date(...today).toDateString());
// Wed Mar 29 2017
function fullName(first = "Hans", last = "Muster") {
return first + ' ' + last;
}
console.log(fullName("Peter")); // "Peter Muster"
let arr = [1, 2, 3];
let [a, b] = arr;
console.log(a); // 1
console.log(b); // 2
let arr = [1, 2, 3, 4, 5];
let [a, , ...rest] = arr;
console.log(a); // 1
console.log(rest); // [3, 4, 5]
let obj = {
firstName: "Hans",
lastName: "Muster",
hobbies: ["Tennis", "Lesen"]
};
let {firstName, hobbies} = obj;
console.log(firstName); // "Hans"
console.log(hobbies); // ["Tennis", "Lesen"]
let {firstName: vorname} = obj;
console.log(vorname); // "Hans"
let func = function() {
// mach etwas
}
// nicht anonym - praktisch beim debuggen
let func = function funcName() {
// mach etwas
}
// seit ES6
let es6Func = () => console.log("Hello");
es6Func(); // "Hello"
let add = (a, b) => a + b;
console.log(add(3, 4)); // 7
// Bei einem Parameter ohne Klammern möglich
let potenz = n => n * n;
console.log(potenz(4)); // 16
let addThree = (a) => (b) => (c) => a + b + c;
console.log(addThree(1)(4)(3)); // 8
// oder in ES5
var addThreeOld = function addThreeOld(a) {
return function(b) {
return function (c) {
return a + b + c;
}
};
};
console.log(addThreeOld(1)(4)(3)); // 8
// Ohne Klammern ;-)
let addThree = a => b => c => a + b + c;
function renderCustomer(c) {
console.log(c); // "Hans Muster"
}
function getFullName(name, callback) {
let lastName = 'Muster';
callback(name + ' ' + lastName);
}
getFullName("Hans", function(result) {
return renderCustomer(result);
});
getFullName("Hans", renderCustomer);
let customer = getCustomer();
if(customer === null || customer === undefined) {
// do sth.
}
if(!customer) {
// do sth.
}
class CustomerComponent {
constructor(private service: Service) {
}
findAll() {
return this.service.findAll();
}
}
class CustomerService {
private _customers: Customer[] = [{name: "Hans"}];
get customers() {
return this._customers;
}
set customers(customers: Customer[]) {
this._customers = customers;
}
extractName(customer: Customer) {
return customer.name;
}
getCustomerNames() {
return this.customers.map(extractName);
}
}
Typ Java | Typ TypeScript |
---|---|
Object | any |
void | void |
boolean | boolean |
int, long, short... | number |
String, char | string |
Type[] | Type[] / Array<Type> |
interface Customer {
hobbies: string[]
}
let validCus: Customer = {
hobbies: ["Tennis", "Squash"]
}
// Type '{ hobbies: string; }' is not assignable to
// type 'Customer'. Types of property 'hobbies' are
// incompatible. Type 'string' is not assignable
// to type 'string[]'.
let compileError: Customer = {
hobbies: "Tennis"
}
interface Customer {
mandatory: string;
optional?: string[]
}
let validCus: Customer = {
mandatory: "Mandatory"
}
// type '{ optional: string; }' is not assignable to
// type 'Customer'. Property 'mandatory' is missing
// in type '{ optional: string; }'.
let compileError: Customer = {
optional: ["Optional", "Prop"]
}
interface Customer {
mandatory: string;
optional?: string[];
fullName: (first: string, last: string) => string;
}
let customer: Customer = {
mandatory: "Hans",
fullName(first, last) {
return first + " " + last;
}
}
function printAge(age: number | string) {
console.log(age);
}
printAge(1); // 1
printAge("1"); // 1
// Argument of type 'number[]' is not assignable
// to parameter of type 'string | number'.
printAge([1, 2, 3])
class Service {
private url = "/api";
createFullUrl(endpoint: number) {
//Type 'number' is not assignable to type 'string'
this.url = endpoint;
return 'localhost' + this.url;
}
getCustomerUrl() {
// Argument of type 'string' is not
// assignable to parameter of type 'number'.
return this.createFullUrl('customer');
}
}
export const API_URL = '/api';
export interface Person = {
name: string;
}
export function add(a: number, b: number) {
return a + b;
}
export const addFive(a: number) => a + 5;
export class Customer {
name: string;
constructor() {}
}
// wie Destructuring ;-)
import {Customer, Person} from "./customer";
import {Component} from "@angular/core";
let customer = new Customer();
let person: Person = {
name: "Hans"
}
import 'rxjs/add/operator/map';
import * as alias from "./exports";
let addFunc = alias.addFive;
$> node -v
$> npm -v
Aktuelle node.js Version: v8.9.1
Aktuelle npm version: 5.5.1
Installiere das Angular CLI über npm
$> npm install -g @angular/cli
Auf -g achten → Globale Installation
$> ng help