Updated.
This commit is contained in:
parent
7f93a474c9
commit
931a546932
263
README.md
263
README.md
|
|
@ -55,13 +55,13 @@ If `your.cola` depends on other scripts in `your.html`, better to notice `browse
|
||||||
Overview
|
Overview
|
||||||
===
|
===
|
||||||
|
|
||||||
## Need to know:
|
### Need to know
|
||||||
- not always valid-javascript is valid-colascript
|
- not always valid-javascript is valid-colascript
|
||||||
- semicolon is always required
|
- semicolon is always required
|
||||||
- in present time typing is just syntax
|
- in present time typing is just syntax
|
||||||
|
|
||||||
|
|
||||||
## Variables
|
### Variables
|
||||||
In current version you can set any type which you want, frankly speaking is not good.. In future we will have static typing.
|
In current version you can set any type which you want, frankly speaking is not good.. In future we will have static typing.
|
||||||
|
|
||||||
var num = 1; // valid JavaScript
|
var num = 1; // valid JavaScript
|
||||||
|
|
@ -74,7 +74,7 @@ In ColaScript, like in CoffeScript, exists boolean-aliases:
|
||||||
no == off == false
|
no == off == false
|
||||||
|
|
||||||
|
|
||||||
## Strings
|
### Strings
|
||||||
We have templating! We can paste variable in string:
|
We have templating! We can paste variable in string:
|
||||||
|
|
||||||
console.log("my login in twitter \@@twLogin");
|
console.log("my login in twitter \@@twLogin");
|
||||||
|
|
@ -110,7 +110,7 @@ Any string in ColaScript is multiline:
|
||||||
align goes by closing-quote.
|
align goes by closing-quote.
|
||||||
|
|
||||||
|
|
||||||
## RegExps
|
### RegExps
|
||||||
Modifer `x` skips whitespaces and new-line same as if you use multiline RegExp:
|
Modifer `x` skips whitespaces and new-line same as if you use multiline RegExp:
|
||||||
|
|
||||||
RegExp url = /
|
RegExp url = /
|
||||||
|
|
@ -122,7 +122,7 @@ Modifer `x` skips whitespaces and new-line same as if you use multiline RegExp:
|
||||||
$/;
|
$/;
|
||||||
|
|
||||||
|
|
||||||
## Arrays
|
### Arrays
|
||||||
Most of array features was taken from CoffeeScript:
|
Most of array features was taken from CoffeeScript:
|
||||||
|
|
||||||
Array arr = [0..9]; // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
Array arr = [0..9]; // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||||
|
|
@ -141,7 +141,7 @@ absolutely new feature is:
|
||||||
console.log(arr[]); // 22
|
console.log(arr[]); // 22
|
||||||
|
|
||||||
|
|
||||||
## Functions
|
### Functions
|
||||||
You have opportunity to declarate functions without `function` keyword, with and without type:
|
You have opportunity to declarate functions without `function` keyword, with and without type:
|
||||||
|
|
||||||
function f1(){
|
function f1(){
|
||||||
|
|
@ -152,7 +152,7 @@ You have opportunity to declarate functions without `function` keyword, with and
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
f2(){
|
f3(){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -198,9 +198,8 @@ All `main` functions in root namespace will bind on `DOMContentLoaded` event:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
## Operators:
|
### Operators
|
||||||
|
- `isset` operator:
|
||||||
- `isset` operator, `??` alternative - which is better?
|
|
||||||
|
|
||||||
bool a = true, b;
|
bool a = true, b;
|
||||||
console.log(isset a ? "seted" : "not seted"); // seted
|
console.log(isset a ? "seted" : "not seted"); // seted
|
||||||
|
|
@ -219,97 +218,149 @@ All `main` functions in root namespace will bind on `DOMContentLoaded` event:
|
||||||
|
|
||||||
- CoffeeScript's modulo operator:
|
- CoffeeScript's modulo operator:
|
||||||
|
|
||||||
int modulo = 5 %% 3; // 2
|
Array nums = [0..9];
|
||||||
|
console.log(nums[4 %% nums.length]); // 4
|
||||||
|
console.log(nums[14 %% nums.length]); // 4
|
||||||
|
|
||||||
- Existential assignment from CoffeeScript:
|
- Existential assignment from CoffeeScript:
|
||||||
|
|
||||||
var undef, def = 5;
|
defaults(Object input, Object defaults){
|
||||||
|
for(int key in defaults) input[key] ?= defaults[key];
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
def ?= undef; // def == 5
|
console.log(defaults({
|
||||||
undef = 6;
|
compress: true
|
||||||
def ?= undef; // def == 6
|
}, {
|
||||||
|
compress: false,
|
||||||
|
mangle: false
|
||||||
|
})); // { compress: true, mangle: false }
|
||||||
|
|
||||||
- `a ? b`, status: done
|
- Existential operator:
|
||||||
|
|
||||||
var a = undefined, b = 3;
|
int a, b = 3;
|
||||||
|
|
||||||
a ? b == 3;
|
console.log(a ? b); // 3
|
||||||
a = 11;
|
a = 11;
|
||||||
a ? b == 1;
|
console.log(a ? b); // 11
|
||||||
|
|
||||||
- `is`, status: done
|
- Binary operator `is`:
|
||||||
|
|
||||||
bool isRegExp = /[^\d]+/g is RegExp; // true
|
bool isRegExp = /[^\d]+/g is RegExp; // true
|
||||||
|
|
||||||
- `isnt`, status: done
|
- Binary operator `isnt`:
|
||||||
|
|
||||||
bool isntString = 3.14 isnt String; // true
|
bool isntString = 3.14 isnt String; // true
|
||||||
|
|
||||||
|
|
||||||
|
### Constructions
|
||||||
|
In ColaScript you can use next syntax with object, to not repeat var names:
|
||||||
|
|
||||||
|
String name = "dangreen";
|
||||||
|
int age = 19;
|
||||||
|
String about = "
|
||||||
|
|
||||||
|
Web programmer, NSTU student.
|
||||||
|
|
||||||
|
";
|
||||||
|
|
||||||
|
Object info = { name, age, about };
|
||||||
|
|
||||||
|
Also you can use `destructuring assignment`:
|
||||||
|
|
||||||
|
int a = 2, b = 3;
|
||||||
|
|
||||||
|
// swap
|
||||||
|
[a, b] = [b, a];
|
||||||
|
|
||||||
|
// with object
|
||||||
|
{ name, age, about, friends : [firstFriend] } = info;
|
||||||
|
|
||||||
|
// with array
|
||||||
|
[firstSkill, ..., lastSkill] = skills;
|
||||||
|
|
||||||
|
From Dart we took `cascade operator`:
|
||||||
|
|
||||||
|
document.querySelector("#myelement").style
|
||||||
|
..fontSize = "16px"
|
||||||
|
..color = "black";
|
||||||
|
|
||||||
|
but we made some modification:
|
||||||
|
|
||||||
|
document.querySelector("#myelement")
|
||||||
|
..style:
|
||||||
|
..fontSize = "16px"
|
||||||
|
..color = "black";
|
||||||
|
..innerHTML = "Hello World!";
|
||||||
|
|
||||||
|
|
||||||
### Multiple
|
As in CoffeeScript, you can use `chained comprassions`:
|
||||||
- `..:`, status: done
|
|
||||||
|
|
||||||
Object profile = {
|
if( 1 < x < 100 ) console.log("x E ( 1 ; 100 )");
|
||||||
name : "dan",
|
|
||||||
nick : "dan",
|
`inline conditions`:
|
||||||
friends : [
|
|
||||||
{ name : "eric", nick : "eric" }
|
String name =
|
||||||
],
|
if (sex == "male") "Dan"
|
||||||
"info" : "coder"
|
else if (sex == "female") "Dasha"
|
||||||
|
else "none";
|
||||||
|
|
||||||
|
`inline switch` and `switch without expression`:
|
||||||
|
|
||||||
|
String grade = switch {
|
||||||
|
when score < 60: 'F'
|
||||||
|
when score < 70: 'D'
|
||||||
|
when score < 80: 'C'
|
||||||
|
when score < 90: 'B'
|
||||||
|
default: 'A'
|
||||||
}
|
}
|
||||||
..name += "iil"
|
|
||||||
..nick += "green"
|
|
||||||
..friends[0]:
|
|
||||||
..name = profile.friends[0].name.capitalize()
|
|
||||||
..nick += "bro";
|
|
||||||
..info += ", student";
|
|
||||||
|
|
||||||
|
As you see you can use keyword `when`, its like `case`, but if the condition is satisfied, `switch` will `break`.
|
||||||
|
|
||||||
- `a > b > c`, status: done
|
### Compiler commands
|
||||||
|
- `@require`, pushed required code to front
|
||||||
if( 0 < x < 100 ) console.log("x E (0; 100)");
|
|
||||||
|
|
||||||
|
|
||||||
### Compiler
|
|
||||||
- `@require`, status: done
|
|
||||||
|
|
||||||
@require "./library/jquery.js" "./library/underscore.js"
|
@require "./library/jquery.js" "./library/underscore.js"
|
||||||
|
|
||||||
- `@include`, status: done
|
- `@include`, insert included code on `@include` place
|
||||||
|
|
||||||
@include "./app/my.js"
|
@include "./app/my.js"
|
||||||
|
|
||||||
- `@use`, status: done
|
- `@use`, enable one of mods
|
||||||
|
|
||||||
@use strict
|
@use strict
|
||||||
@use asmjs
|
@use asm
|
||||||
@use closure
|
@use closure
|
||||||
|
|
||||||
|
`@use closure` wrapping code in closue:
|
||||||
|
|
||||||
## Expressions
|
// cola
|
||||||
- `switch` assignment, status: done!
|
|
||||||
|
|
||||||
String weather = switch(temperature){
|
@use closure
|
||||||
when -10: 'cold';
|
|
||||||
when 20: 'normal';
|
NgModule app = angular.module('app', []);
|
||||||
when 35: 'hot';
|
|
||||||
};
|
// js
|
||||||
|
|
||||||
|
(function(){
|
||||||
|
NgModule app = angular.module('app', []);
|
||||||
|
})();
|
||||||
|
|
||||||
|
Also you can use multiple closures in one file:
|
||||||
|
|
||||||
|
@use closure {
|
||||||
|
int a = 123;
|
||||||
|
}
|
||||||
|
|
||||||
|
@use closure {
|
||||||
|
int a = 321;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
## Vars
|
Future
|
||||||
|
===
|
||||||
- multiple assignment, status: done
|
- static typing
|
||||||
|
|
||||||
[a, b, c] = [b, c, a];
|
|
||||||
var {poet: {String name, address: [street, city]}} = futurists;
|
|
||||||
[a, ..., b] = someArray;
|
|
||||||
|
|
||||||
## Classes
|
|
||||||
- classes
|
- classes
|
||||||
- singletones
|
|
||||||
- injectors
|
|
||||||
|
|
||||||
class A {
|
class A {
|
||||||
|
|
||||||
|
|
@ -342,6 +393,18 @@ All `main` functions in root namespace will bind on `DOMContentLoaded` event:
|
||||||
set some(val) => about += val;
|
set some(val) => about += val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- classes and typing with templates
|
||||||
|
|
||||||
|
class A<T> {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
|
Array<int> arr = [0...10];
|
||||||
|
Object<String, String> obj = { name: "Eric" };
|
||||||
|
|
||||||
|
|
||||||
|
- singletones
|
||||||
|
|
||||||
singleton S { // in fact this is object
|
singleton S { // in fact this is object
|
||||||
int x = 45;
|
int x = 45;
|
||||||
String s = "txt";
|
String s = "txt";
|
||||||
|
|
@ -353,11 +416,10 @@ All `main` functions in root namespace will bind on `DOMContentLoaded` event:
|
||||||
int operator[](int index) => index + 584;
|
int operator[](int index) => index + 584;
|
||||||
operator[]=(int index, int val) => x = index + val;
|
operator[]=(int index, int val) => x = index + val;
|
||||||
|
|
||||||
String operator.(String key) => key + "!";
|
|
||||||
operator.(String key, String value) => s = "@key @value";
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- injectors
|
||||||
|
|
||||||
injector String {
|
injector String {
|
||||||
String replaceAll(a, b){
|
String replaceAll(a, b){
|
||||||
String res = this;
|
String res = this;
|
||||||
|
|
@ -366,10 +428,75 @@ All `main` functions in root namespace will bind on `DOMContentLoaded` event:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// or
|
- declaration function by object property
|
||||||
|
|
||||||
String String::replaceAll(a, b){
|
String String::replaceAll(a, b){
|
||||||
String res = this;
|
String res = this;
|
||||||
while(res.indexOf(a) != -1) res = res.replace(a, b);
|
while(res.indexOf(a) != -1) res = res.replace(a, b);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// or
|
||||||
|
|
||||||
|
Object data = someData;
|
||||||
|
int data.getFriendsCount() => this.friends.length;
|
||||||
|
|
||||||
|
- destructed function arguments
|
||||||
|
|
||||||
|
test({String name, String login, String photoUrl}){
|
||||||
|
console.log(name, login, photoUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
- ES6 `for`
|
||||||
|
|
||||||
|
for({String name, String login, String photoUrl} in usesr){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
for(name of names){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
- asm.js native syntax, for example
|
||||||
|
|
||||||
|
// cola
|
||||||
|
// ...
|
||||||
|
|
||||||
|
@use asm
|
||||||
|
|
||||||
|
int f(double j){
|
||||||
|
int i = 1;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// js
|
||||||
|
// ...
|
||||||
|
|
||||||
|
"use asm";
|
||||||
|
|
||||||
|
function f(j){
|
||||||
|
j = +j;
|
||||||
|
var i = 1|0;
|
||||||
|
return i|0;
|
||||||
|
}
|
||||||
|
|
||||||
|
- plugin api to making native syntax for libraries and frameworks, example:
|
||||||
|
|
||||||
|
class MyComponent extends PolymerComponent {
|
||||||
|
String tagname = "my-component";
|
||||||
|
|
||||||
|
ready(){
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
to
|
||||||
|
|
||||||
|
Polymer('my-component', {
|
||||||
|
ready: function(){
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user