Coding JavaScript the expressive way Learning & Development Telerik Software Academy.

Slides:



Advertisements
Similar presentations
Windows Basic and Dynamic Disk Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator Marian Marinov CEO of 1H Ltd.
Advertisements

HTML Forms, GET, POST Methods Tran Anh Tuan Edit from Telerik Academy
Make swiftly iOS development Telerik Academy Telerik Academy Plus.
Amazon S 3, App Engine Blobstore, Google Cloud Storage, Azure Blobs Svetlin Nakov Telerik Software Academy academy.telerik.com.
RPN and Shunting-yard algorithm Ivaylo Kenov Telerik Software Academy academy.telerik.com Technical Assistant
Shortest paths in edge-weighted digraph Krasin Georgiev Technical University of Sofia g.krasin at gmail com Assistant Professor.
Telerik Software Academy Telerik School Academy.
Asynchronous Programming with C# and WinRT
Unleash the Power of JavaScript Tooling Telerik Software Academy End-to-end JavaScript Applications.
Touch and Gestures with Xamarin Forms
Character sequences, C-strings and the C++ String class, Working with Strings Learning & Development Team Telerik Software Academy.
Hybrid or Native?! Doncho Minkov Telerik Software Academy Senior Technical Trainer
Done already for your convenience! Telerik School Academy Unity 2D Game Development.
Processing Sequences of Elements Telerik School Academy C# Fundamentals – Part 1.
With Mocha and Karma Telerik Academy Telerik Software Academy.
C# Fundamentals – Part I
Welcome to the JSON-stores world Telerik Software Academy Databases.
The Business Plan and the Business Model Margarita Antonova Volunteer Telerik Academy academy.telerik.com Business System Analyst Telerik Corporation.
What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team Telerik Software Academy.
Making JavaScript code by template! Learning & Development Team Telerik Software Academy.
Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training Who, What, Why?
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Processing Matrices and Multidimensional Tables Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Learning & Development Telerik Software Academy.
Telerik Software Academy ASP.NET Web Forms.
Classical OOP in JavaScript Classes and stuff Telerik Software Academy
Optimization problems, Greedy Algorithms, Optimal Substructure and Greedy choice Learning & Development Team Telerik Software.
Using Selenium for Mobile Web Testing Powered by KendoUI Telerik QA Academy Atanas Georgiev Senior QA Engineer KendoUI Team.
New features: classes, generators, iterators, etc. Telerik Academy Plus JavaScript.Next.
Throwing and Catching Exceptions Tran Anh Tuan Edit from Telerik Software Academy
Past, Present and Future Nikolay Kostov Telerik Software Academy academy.telerik.com Team Lead, Senior Developer and Trainer
Loops, Conditional Statements, Functions Tran Anh Tuan Edit from Telerik Academy
Private/Public fields, Module, Revealing Module Learning & Development Team Telerik Software Academy.
Building Data-Driven ASP.NET Web Forms Apps Telerik Software Academy ASP.NET Web Forms.
Course Introduction Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Telerik Software Academy End-to-end JavaScript Applications.
General and reusable solutions to common problems in software design Vasil Dininski Telerik Software Academy academy.telerik.com Intern at Telerik Academy.
Planning and Tracking Software Quality Yordan Dimitrov Telerik Corporation Team Leader, Team Pulse, Team Leader, Team Pulse, Telerik Corporation,
Closures, Function Scope, Nested Functions Learning & Development Team Telerik Software Academy.
What you need to know Ivaylo Kenov Telerik Corporation Telerik Academy Student.
Data binding concepts, Bindings in WinJS George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net.
Pavel Kolev Telerik Software Academy Senior.Net Developer and Trainer
Objects, Properties, Primitive and Reference Types Learning & Development Team Telerik Software Academy.
When and How to Refactor? Refactoring Patterns Alexander Vakrilov Telerik Corporation Senior Developer and Team Leader.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Doing the Canvas the "easy way"! Learning & Development Telerik Software Academy.
Creating and Running Your First C# Program Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Creating Graphics for the Web Learning & Development Team Telerik Software Academy.
Course Overview Doncho Minkov Telerik Software Academy Technical Trainer
Correctly Formatting the Source Code Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
Data Types, Primitive Types in C++, Variables – Declaration, Initialization, Scope Telerik Software Academy academy.telerik.com Learning and Development.
The past, the present, the future Learning & Development Team Telerik Software Academy.
Learn to Design Error Steady Code Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Connecting, Queries, Best Practices Tran Anh Tuan Edit from Telerik Software Academy
Processing Sequences of Elements Telerik Software Academy C# Fundamentals – Part 2.
Telerik JavaScript Framework Telerik Software Academy Hybrid Mobile Applications.
Telerik Software Academy Databases.
Things start to get serious Telerik Software Academy JavaScript OOP.
Learning & Development Mobile apps for iPhone & iPad.
Processing Matrices and Multidimensional Tables Telerik Software Academy C# Fundamentals – Part 2.
Nikolay Kostov Telerik Software Academy academy.telerik.com Team Lead, Senior Developer and Trainer
Functions and Function Expressions Closures, Function Scope, Nested Functions Telerik Software Academy
Implementing Control Logic in C# Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical trainer
Inheritance, Abstraction, Encapsulation, Polymorphism Telerik Software Academy Mobile apps for iPhone & iPad.
Mocking tools for easier unit testing Telerik Software Academy High Quality Code.
What why and how? Telerik School Academy Unity 2D Game Development.
Windows Security Model Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Definition, Constructors, Methods, Access Modifiers, Static/Instance members, Learning & Development Team Telerik Software Academy.
Presentation transcript:

Coding JavaScript the expressive way Learning & Development Telerik Software Academy

 JavaScript-like languages  TypeScript & CoffeeScript  CoffeeScript Overview  Installation & Compilation  CoffeeScript Features  Variables and functions, string interpolation  Conditionals, Loops and Comprehensions  Operations, Aliases, Destructuring assignments  Classes & Inheritance

Languages that compile to JavaScript

 JavaScript is a not-mean-to-be-first-class- language  It was developed by Netscape for updating the UI of a web page  In result, many languages appeared that compile to JavaScript  CoffeeScript and TypeScript are most famous

Installation and compilation

 CoffeeScript has its own core compiler, and can even run inside the browser  Yet these are hard and slow  CoffeeScript can be installed using Node.js:  And then compiled using: $ npm install –g coffee-script $ coffee –-compile scripts.coffee

Live Demo

 Since CoffeeScript is widely used most of the Tools for JavaScript development support CoffeeScript as well:  Visual Studio – Web Essentials  Sublime text – Better CoffeeScript  WebStorm – built-in

Live Demo

What can we do with CoffeeScript?

 CoffeeScript is a whole new language to learn  Yet, has parts of Python and JavaScript  Omit semicolons and brackets  But the indentation matters a lot!  They introduce scope  This is a valid CoffeeScript code: person = name: 'Peter' name: 'Peter' age: 17 age: 17 console.log person CoffeeScript

 CoffeeScript is a whole new language to learn  Yet, has parts of Python and JavaScript  Omit semicolons and brackets  But the indentation matters a lot!  They introduce scope  This is a valid CoffeeScript code: person = name: 'Peter' name: 'Peter' age: 17 age: 17 console.log person var person; person = { name: 'Peter', name: 'Peter', age: 17 age: 17};console.log(person); Compiles to CoffeeScript JavaScript

 In CoffeeScript all functions are expressions  No function declarations  Created like C# LAMBDA expressions: sum = (numbers) -> sum = 0 sum = 0 for n in numbers for n in numbers sum += n sum += n return sum return sumCoffeeScript

 In CoffeeScript all functions are expressions  No function declarations  Created like C# LAMBDA expressions: sum = (numbers) -> sum = 0 sum = 0 for n in numbers for n in numbers sum += n sum += n return sum return sum var sum; sum = function(numbers){ var sum, n, i; var sum, n, i; sum = 0; sum = 0; for(i = 0; i < numbers.length; i++){ for(i = 0; i < numbers.length; i++){ n = numbers[i]; n = numbers[i]; sum += n; sum += n; } return sum; return sum;}CoffeeScriptJavaScript Compiles to

Live Demo

 When creating Objects and/or arrays curly brackets and colons can be omitted  Most of the time… student = names: names: first: 'Peter' first: 'Peter' last: 'Petrov' last: 'Petrov' marks: [ marks: [ ]CoffeeScript

 When creating Objects and/or arrays curly brackets and colons can be omitted  Most of the time… student = names: names: first: 'Peter' first: 'Peter' last: 'Petrov' last: 'Petrov' marks: [ marks: [ ] var student; student = { names: { names: { first: 'Peter', first: 'Peter', last: 'Petrov' last: 'Petrov' }, }, marks: [5.5, 6.0, 4.5] marks: [5.5, 6.0, 4.5]}; CoffeeScript JavaScript Compiles to

Live Demo

 CoffeeScript is highly expressional language  And conditional statements can be done in many ways  Sometimes they are compiled to ternary expressions  CoffeeScript also adds extensions:  unless meaning if not : unless condition # same as if not condition

 CoffeeScript conditional statements: if condition # do stuff # do stuff obj = value if condition goToWork() unless todayIsWeekend() Coffee Coffee Coffee

 CoffeeScript conditional statements: if condition # do stuff # do stuff if(condition) { //do stuff //do stuff} obj = value if condition goToWork() unless todayIsWeekend() Coffee JS Coffee Coffee

 CoffeeScript conditional statements: if condition # do stuff # do stuff if(condition) { //do stuff //do stuff} obj = value if condition if(condition) { obj = value; obj = value;} goToWork() unless todayIsWeekend() Coffee JS Coffee JS Coffee

 CoffeeScript conditional statements: if condition # do stuff # do stuff if(condition) { //do stuff //do stuff} obj = value if condition if(condition) { obj = value; obj = value;} goToWork() unless todayIsWeekend() if(!(todayIsWeekend())){ goToWork(); goToWork();} Coffee JS Coffee JS Coffee JS

Live Demo

 CoffeeScript supports all the regular loops: while condition # code # code for item, i in items # code # code while (condition) { # code # code} for(i = 0;i<items.length;i++){ item = items[i]; item = items[i];} for key, value of items item item for (key in items) { value = items[key]; value = items[key]; item; item;} Coffee JS Coffee JSJSJSJS Coffee JSJSJSJS

Live Demo

 Comprehensions in CoffeeScript allows the use of loops as a result  Iterate over a collection and return a result serialized = (serialize item for item in items) Coffee serialized = (function() { var _i, _len, _results; var _i, _len, _results; _results = []; _results = []; for (_i = 0, _len = items.length; _i < _len; _i++) { for (_i = 0, _len = items.length; _i < _len; _i++) { item = items[_i]; item = items[_i]; _results.push(serialize(item)); _results.push(serialize(item)); } return _results; return _results;})();JS

Live Demo

Creating ranges to easify the iteration of arrays

 Ranges in Coffee create a array of number values in a given range: numbers = [1..3] -> numbers = [1, 2, 3] numbers = [1...3] -> numbers = [1, 2] for number in [0..maxNumber] by 2 -> # i =0, 2, 4, … console.log "#{i} is an even number" console.log "#{i} is an even number"

Live Demo

 Arrays can be sliced using ranges: numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] result = numbers[2..3] result = [3, 4, 5] result = numbers[..3] result = [1, 2, 3, 4, 5] result = numbers[6..] result = [7, 8, 9] result = numbers[..] result = clone of numbers numbers[1..2] = [-2, -3] result = [1, -2, -3, 4, …]

Live Demo

 CoffeeScript aims to be as expressional as possible  So many of the operators have aliases: this of x ** n x // y ===!==!&&||truefalsethisin Math.Pow(x, n) Math.floor(x / y) compiles to

Live Demo

 CoffeeScript provides a way to create Functional/Classical OOP class Shape constructor: (x, y) -> constructor: (x, y) = = = = y setX: (newX) -> setX: (newX) = = x getX: () -> getX: var Shape; Shape = (function() { function Shape(x, y) { function Shape(x, y) { this.x = x; this.x = x; this.y = y; this.y = y; } Shape.prototype.setX=function(newX){ Shape.prototype.setX=function(newX){ return this.x = x; return this.x = x; }; }; Shape.prototype.getX = function() { Shape.prototype.getX = function() { return this.x; return this.x; }; }; return Shape; return Shape;})();

 CoffeeScript provides a way to create Functional/Classical OOP class Shape constructor: (x, y) -> constructor: (x, y) = = = = y setX: (newX) -> setX: (newX) = = x getX: () -> getX: var Shape; Shape = (function() { function Shape(x, y) { function Shape(x, y) { this.x = x; this.x = x; this.y = y; this.y = y; } Shape.prototype.setX=function(newX){ Shape.prototype.setX=function(newX){ return this.x = x; return this.x = x; }; }; Shape.prototype.getX = function() { Shape.prototype.getX = function() { return this.x; return this.x; }; }; return Shape; return Shape;})(); The constructor is compiled into function constructor

 CoffeeScript provides a way to create Functional/Classical OOP class Shape constructor: (x, y) -> constructor: (x, y) = = = = y setX: (newX) -> setX: (newX) = = x getX: () -> getX: var Shape; Shape = (function() { function Shape(x, y) { function Shape(x, y) { this.x = x; this.x = x; this.y = y; this.y = y; } Shape.prototype.setX=function(newX){ Shape.prototype.setX=function(newX){ return this.x = x; return this.x = x; }; }; Shape.prototype.getX = function() { Shape.prototype.getX = function() { return this.x; return this.x; }; }; return Shape; return Shape;})(); Methods are attached to the prototype

Live Demo

 CoffeeScript supports also inheritance in a Classical OOP way  Using the keyword extends  Providing a super() method to call from parent class Shape constructor: (x, y) -> constructor: (x, y) -> setX setX getX: () -> getX: () -> class Rect extends Shape constructor: (x, y, w, h) -> constructor: (x, y, w, h) -> super x, y super x, = = = = h

Live Demo

Putting executable code inside a string

 String interpolation allows to execute script and put the result inside a string  Use double quotes ( " " ) and #{script} class Person -> -> introduce: () -> """Hi! I am introduce: () -> """Hi! I am Person = (function() { … Person.prototype.introduce = function() { Person.prototype.introduce = function() { return "Hi! I am " + this.fname + " " + this.lname + "."; return "Hi! I am " + this.fname + " " + this.lname + "."; }; }; return Person; return Person;})();

 String interpolation allows to execute script and put the result inside a string  Use double quotes ( " " ) and #{script} class Person -> -> introduce: () -> """Hi! I am introduce: () -> """Hi! I am Person = (function() { … Person.prototype.introduce = function() { Person.prototype.introduce = function() { return "Hi! I am " + this.fname + " " + this.lname + "."; return "Hi! I am " + this.fname + " " + this.lname + "."; }; }; return Person; return Person;})();

 String interpolation allows to execute script and put the result inside a string  Use double quotes ( " " ) and #{script} class Person -> -> introduce: () -> """Hi! I am introduce: () -> """Hi! I am Person = (function() { … Person.prototype.introduce = function() { Person.prototype.introduce = function() { return "Hi! I am " + this.fname + " " + this.lname + "."; return "Hi! I am " + this.fname + " " + this.lname + "."; }; }; return Person; return Person;})(); Triple quotes create strings on multiple lines

Live Demo

 CoffeeScript introduces a more expressive way for creating switch-case blocks switch day when 'Mon' then console.log 'The week starts' when 'Mon' then console.log 'The week starts' when 'Tue' then console.log '''Still accommodating to the when 'Tue' then console.log '''Still accommodating to the start of the week''' start of the week''' when 'Wed' then console.log 'Time to work!' when 'Wed' then console.log 'Time to work!' when 'Thu' then console.log 'Well… end of the week is near' when 'Thu' then console.log 'Well… end of the week is near' when 'Fri' then console.log 'Day of the master.' when 'Fri' then console.log 'Day of the master.' when 'Sat', 'Sun' then console.log 'Relax' when 'Sat', 'Sun' then console.log 'Relax' else console.log 'Wrong day…' else console.log 'Wrong day…'

 CoffeeScript introduces a more expressive way for creating switch-case blocks switch day when 'Mon' then console.log 'The week starts' when 'Mon' then console.log 'The week starts' when 'Tue' then console.log '''Still accommodating to the when 'Tue' then console.log '''Still accommodating to the start of the week''' start of the week''' when 'Wed' then console.log 'Time to work!' when 'Wed' then console.log 'Time to work!' when 'Thu' then console.log 'Well… end of the week is near' when 'Thu' then console.log 'Well… end of the week is near' when 'Fri' then console.log 'Day of the master.' when 'Fri' then console.log 'Day of the master.' when 'Sat', 'Sun' then console.log 'Relax' when 'Sat', 'Sun' then console.log 'Relax' else console.log 'Wrong day…' else console.log 'Wrong day…' else in CoffeeScript is like default in Javascript

 CoffeeScript introduces a more expressive way for creating switch-case blocks switch day when 'Mon' then console.log 'The week starts' when 'Mon' then console.log 'The week starts' when 'Tue' then console.log '''Still accommodating to the when 'Tue' then console.log '''Still accommodating to the start of the week''' start of the week''' when 'Wed' then console.log 'Time to work!' when 'Wed' then console.log 'Time to work!' when 'Thu' then console.log 'Well… end of the week is near' when 'Thu' then console.log 'Well… end of the week is near' when 'Fri' then console.log 'Day of the master.' when 'Fri' then console.log 'Day of the master.' when 'Sat', 'Sun' then console.log 'Relax' when 'Sat', 'Sun' then console.log 'Relax' else console.log 'Wrong day…' else console.log 'Wrong day…' mark = switch when score < 100 then 'Failed' when score < 100 then 'Failed' when score < 200 then 'Success' when score < 200 then 'Success' else 'Excellent!' else 'Excellent!'  And they can be used as expressions:

 CoffeeScript introduces a more expressive way for creating switch-case blocks switch day when 'Mon' then console.log 'The week starts' when 'Mon' then console.log 'The week starts' when 'Tue' then console.log '''Still accommodating to the when 'Tue' then console.log '''Still accommodating to the start of the week''' start of the week''' when 'Wed' then console.log 'Time to work!' when 'Wed' then console.log 'Time to work!' when 'Thu' then console.log 'Well… end of the week is near' when 'Thu' then console.log 'Well… end of the week is near' when 'Fri' then console.log 'Day of the master.' when 'Fri' then console.log 'Day of the master.' when 'Sat', 'Sun' then console.log 'Relax' when 'Sat', 'Sun' then console.log 'Relax' else console.log 'Wrong day…' else console.log 'Wrong day…' mark = switch when score < 100 then 'Failed' when score < 100 then 'Failed' when score < 200 then 'Success' when score < 200 then 'Success' else 'Excellent!' else 'Excellent!'  And they can be used as expressions: Assigns the correct value to mark

Live Demo

 Destructuring assignments are used to extract values from arrays or objects findMaximums = (numbers) -> min = numbers [0] min = numbers [0] max = numbers [0] max = numbers [0] for number in numbers for number in numbers max = number if max < number max = number if max < number min = number if number < min min = number if number < min [min, max] [min, max] [min, max] = findMaximums [1, -2, 3, -4, 5, -6] # min will have value -6 and max will have value 5

 Destructuring assignments are used to extract values from arrays or objects findMaximums = (numbers) -> min = numbers [0] min = numbers [0] max = numbers [0] max = numbers [0] for number in numbers for number in numbers max = number if max < number max = number if max < number min = number if number < min min = number if number < min [min, max] [min, max] [min, max] = findMaximums [1, -2, 3, -4, 5, -6] # min will have value -6 and max will have value 5 Assigns the corresponding values to min and max

Live Demo

 Destructuring assignments are used to extract values from arrays or objects class Person constructor: -> constructor: = name.split ' = name.split ' ' person = new Person 'Peter Petrov', 17 {fname, age} = person

 Destructuring assignments are used to extract values from arrays or objects class Person constructor: -> constructor: = name.split ' = name.split ' ' person = new Person 'Peter Petrov', 17 {fname, age} = person person = new Person('Peter Petrov', 17); fname = person.fname, age = person.age; Compiles to

Live Demo

форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop уроци по програмиране и уеб дизайн за ученици ASP.NET MVC курс – HTML, SQL, C#,.NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания ASP.NET курс - уеб програмиране, бази данни, C#,.NET, ASP.NET курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране