Section 1

Preview this deck

Lambda Expressions

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (34)

Section 1

(34 cards)

Lambda Expressions

Front

A lambda that has a single line of code . The part after the => is returned. var foo = (x:number)=>10 + x

Back

Function Constructor

Front

You can also use the `new` constructor keyword to make a function. var myFunction = new Function("a", "b", "return a * b"); var x = myFunction(4, 3); console.log(x);

Back

Class

Front

Standard OOP class. Use `class` keyword Can contain: - Fields/Properties - Constructor - Functions Uses the `this` keyword for properties of current instance Access properties and functions using dot notation. Inherit from another using `extends`. Implement an interface using `implements` class Car { //field engine:string; //constructor constructor(engine:string) { this.engine = engine } //function disp():void { console.log("Engine is : "+this.engine) } } var myCar = new Car("v6") console.log(car.engine) car.disp()

Back

Anonymous Functions

Front

A function without a function name. Gets built and stored with a variable. var msg = function() { return "hello world"; }

Back

Default Function Parameters

Front

Just put = after the type It cannot be optional and default at the same time

Back

Interface Inheritance

Front

Just use the extends keyword in declaration. Multiple inheritance is allowed using comma. interface Person { age:number } interface Musician extends Person { instrument:string } var drummer = <Musician>{}; drummer.age = 27 drummer.instrument = "Drums"

Back

Duck typing

Front

Two objects are considered to be of the same type if both share the same set of properties. Allows us to pass anonymous objects if they have the same set of properties interface IPoint { x:number y:number } function addPoints(p1:IPoint,p2:IPoint):IPoint { var x = p1.x + p2.x var y = p1.y + p2.y return {x:x,y:y} } //Valid var newPoint = addPoints({x:3,y:4},{x:5,y:1}) //Error var newPoint2 = addPoints({x:1},{x:4,y:3})

Back

Function Overloading

Front

Have a function with the same name but allow different input variations. Syntax: - First define the overloads - Then define the function using any type Can change 3 things when overloading - Data type - Number of params - Order of params Note − The function signature doesn't include the function's return type. function disp(s1:string):void; function disp(n1:number,s1:string):void; function disp(x:any,y?:any):void { console.log(x); console.log(y); } disp("abc") disp(1,"xyz");

Back

Data field visibility

Front

Control visibility by setting one of three types for class fields. - public (default) - private - protected class Encapsulate { str:string = "hello" private str2:string = "world" } var obj = new Encapsulate() console.log(obj.str) //accessible console.log(obj.str2) //compilation Error as str2 is private

Back

Tuple

Front

Like an array but the items inside it can be of any type var mytuple = [10,"Hello","World","typeScript"]; //create a tuple console.log("Tuple value at index 0 "+mytuple[0]) //update a tuple element mytuple[0] = 121 console.log("Tuple value at index 0 changed to "+mytuple[0])

Back

Static class fields

Front

Just use the static keyword

Back

Benefits

Front

- Compile code and discover bugs before the app is run - Strongly typed - Type definitions for libraries so auto completion works (.d.ts extensions) - Supports OOP

Back

Namespace

Front

Group related code to avoid javascript global names space issues New to typescript. It effectively wraps everything inside a self declared function scope. namespace SomeNameSpaceName { export interface ISomeInterfaceName { } export class SomeClassName { } } SomeNameSpaceName.SomeClassName;

Back

Lambda Statement

Front

A full lambda function with a code block var foo = (x:number)=> { x = 10 + x console.log(x) }

Back

Optional Function Parameter

Front

Add ? after the variable name. Must be last input for function. Ex. function foo(a:number, b?:string) { }

Back

Any

Front

The parent type. Means anything

Back

Identifiers

Front

Names for variables, functions, etc... - Cannot begin with a number - Only alphanumeric, _ , and $ - No keywords - Must be unique - Case sensitive - No spaces

Back

Array Interface

Front

Define either: 1 - the index type (only number or string or both) 2 - the value type interface IArray { [index:number]:string } var list2:IArray = ["John",1,"Bran"] //Error cause 1 is not a string

Back

Function Rest Parameters

Front

Allow passing an unknown amount of inputs. Must be the last input for the function. Prefix with 3 dots function foo(...nums:number[]) { //nums is an array of 1,2,3 } foo(1,2,3)

Back

Objects as function parameters

Front

var person = { firstname:"Tom", lastname:"Hanks" }; var invokeperson = function(obj: { firstname:string, lastname :string }) { console.log("first name :"+obj.firstname) console.log("last name :"+obj.lastname) } invokeperson(person) OR an anonymous object invokeperson({ firstname: "Sachin", lastname: "Tendulkar" });

Back

Type Assertion

Front

Change a variables type by putting the type <type> in front of the variable. This does not change it at runtime like "Type Casting". It is only a hint during compile checking. Ex. var str = '1' var str2:number = <number> <any> str //str is now of type number console.log(str2)

Back

Types

Front

The strong types for variables. - `Any` is the super type - Built in types - User-defined types

Back

Interface

Front

Standard OOP interface. A contract for an object, array or class. - Define with keyword `interface`. - Use by specifying the type. interface IPerson { firstName:string, lastName:string, sayHi: ()=>string } var customer:IPerson = { firstName:"Tom", lastName:"Hanks", sayHi: ():string =>{return "Hi there"} }

Back

Lambda Functions in TS

Front

Same as in JS but with the type var foo = (x:number)=>10 + x

Back

Ways to Declare Variable

Front

4 Ways to Declare - Type and Value var a:number = 1; - No Type and Value WITH Inferred Typing var a = 1; //number WITHOUT Inferred var a = 1; // any - Type and No Value (undefined) var a:number; - No Type and No Value (any & undefined) var a;

Back

Functions

Front

function function_name(input:number, inputArray: number[], inputDefault:string="defaultValue", inputOptional?:string):return_type { //statements return value; } NOTE. If you do not put the input type then `any` is assumed

Back

Deconstruct Tuple

Front

Pull out specific items from a tuple and put them in a new variable. var a =[10,"hello"] var [b,c] = a console.log( b ) //10 console.log( c ) //hello

Back

Class Inheritance

Front

Same as interface... just use `extends` keyword * Multiple inheritance is not supported. You can override methods by just declaring them again. Call to the parent classes method using `super.functionName()`

Back

Built In Types

Front

- number - string - boolean - void - null - undefined

Back

Object

Front

Instance of something with key value pairs. Values can be a scalar value, function, or array. You cannot add a function after you declare an object literal since objects must be an instance of a type. Instead make a function template var person = { firstName:"Tom", lastName:"Hanks", sayHello:function() { } //Type template } person.sayHello = function() { console.log("hello "+person.firstName) } person.sayHello()

Back

Semicolon Requirement

Front

Optional at the end of a line. Required if you put two statements on one line.

Back

Deconstruct Object

Front

Pull out specific properties from an object and put them in a new variable. var a = {b: 10, c: "hello"}; var {b} = a; console.log(b) // 10

Back

Union Type

Front

Allows you to specify that a type can be one of many options. Denote with a pipe delimited list var val:string|number; var arr:number[]|string[];

Back

Declaration Files

Front

Allows the intellisense inside the IDE. Uses the .d.ts extension

Back