Командная разработка “толстых клиентов”
DESCRIPTION
- Что такое CoffeeScript и зачем он нужен - Знакомство с синтаксисом и обзор возможностейTRANSCRIPT
Командная разработка“толстых клиентов”
Что нравится в CoffeeScript
● Require● Отступы вместо скобок● Классическое ООП● Отсутствие var, необязательные скобки & return● “Читаемые строки #{var}”● Удобные конструкции для работы с массивами,
объектами, циклами и т.п.● Управляемый контекст “this”
Require
# Business logic and communications.
require './portal/models'
require './portal/routes'
require './portal/server'
# UI.
require './portal/views/helpers'
require './portal/views/header'
require './core/views/template'
…
Classes, Inheritance, and Super
class A constructor: (property) -> # … method: -> # …
class B extends A constructor: -> super("Property value") method: -> super()
Classes, Inheritance, and Supervar A, B, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
A = (function() { function A(property) {} A.prototype.method = function() {}; return A;})();
B = (function(_super) { __extends(B, _super); function B() { B.__super__.constructor.call(this, "Property value"); } B.prototype.method = function() { return B.__super__.method.call(this); }; return B;})(A);
class A constructor: (property) -> # … method: -> # …
class B extends A constructor: -> super("Property value") method: -> super()
Аргументы функций
class Database
connect : (
@host = 'localhost'
@port = 80
options…
callback
) ->
# Тело функции / метода
Аргументы функций
class Database
connect : (
@host = 'localhost'
@port = 80
options…
callback
) ->
# Тело функции / метода
var Database, __slice = [].slice;
Database = (function() { function Database() {}
Database.prototype.connect = function() { var callback, host, options, port, _i; host = arguments[0], port = arguments[1], options = 4 <= arguments.length ? __slice.call(arguments, 2, _i = arguments.length - 1) : (_i = 2, []), callback = arguments[_i++]; this.host = host != null ? host : 'localhost'; this.port = port != null ? port : 80; };
return Database;
})();
Простые if, unless
return if error
port = 8080 unless port
port = if https then 443 else 80
callback?()
Массивы
numbers = [0..9]
n = numbers[3..6]
numbers[3..6] = [-3, -4, -5, -6]
[foo, bar] = [1, 2]
Контекст вызова функций и методов
$('#element').on 'click', (e) ->
@openMenu(e)
$('#element').on 'click', (e) =>
@openMenu(e)
$('#element').on('click', function(e) { return this.openMenu(e);});
var _this = this;
$('#element').on('click', function(e) { return _this.openMenu(e);});
Минусы CoffeeScript
● Новый синтаксис
● Сборка проекта
● Генерирует много кода
● (Отладка)
Алексей Иванов[email protected]: a.ivanov