不支持Closures闭包

  1. //不支持写法
  2. function computeSum(arr: i32[]): i32 {
  3. var sum = 0
  4. arr.forEach(value => {
  5. sum += value // fails
  6. })
  7. return sum
  8. }
  9. //支持写法1
  10. var sum: i32 // becomes a WebAssembly Global
  11. function computeSum(arr: i32[]): i32 {
  12. sum = 0
  13. arr.forEach(value => {
  14. sum += value // works
  15. })
  16. return sum
  17. }
  18. //支持写法2
  19. function computeSum(arr: i32[]): i32 {
  20. var sum = 0
  21. for (let i = 0, k = arr.length; i < k; ++i) {
  22. sum += arr[i] // works
  23. }
  24. return sum
  25. }

不支持Promise异步和async await写法

  1. // 不支持写法
  2. const p = new Promise((resolve, reject)=>{
  3. });
  4. // 不支持写法
  5. async foo():void{
  6. }
  7. await foo();

函数声明必须在全局,不能在函数内

  1. // 不支持写法
  2. function play(): void{
  3. function study(): void{
  4. //....
  5. }
  6. }
  7. // 支持写法
  8. function play(): void{
  9. //..
  10. }
  11. function study(): void{
  12. //....
  13. }

[]在AssemblyScript中

// 不支持
const names = ["xiaoming", 1235]; // 数据类型必须一致

// 支持
const names = ["xiaoming", "xiaowang"];
等同于
const names = new Array<string>();
names.push("xiaoming");
names.push("xiaowang");

{}在AssemblyScript中

// 不支持
const stu = {name:'小明'};

// 支持,必须声明类型
class Student{
  name: string;
}
const stu:Student = {name:'小明'};

没有Object,必须是严格类型对象

// 不支持
var a = {}
a.prop = "hello world"

// 支持
var a = new Map<string,string>()
a.set("prop", "hello world")

// 支持
class A {
  constructor(public prop: string) {}
}
var a = new A("hello world")
// 支持
declare class A{
  prop: string;
}
let a: A = {prop: "hello world"};

number操作

  • JS中数字类型为number
  • as中数字类型是更具体的i32,i64,f64,f32等,as中number默认为f64 ```typescript // float转int const num: number = 1.24; // 取整数(不四舍五入) const int_num: i64 = num as i64; const int_num: i64 = Math.floor(num) as i64;

// 字符串转数字 // 错误写法(坑),parseInt返回值为f64类型, 官网是错的 const int_num: i64 = parseInt(“1.24”); // 正确写法 const int_num: i64 = parseInt(“1.24”) as i64;


<a name="6dWoQ"></a>
#### [不支持any 和 undefined](https://www.assemblyscript.org/stdlib/number.html#integers)
```javascript
// 不支持写法
function foo(a?) {
  var b = a + 1
  return b
}

// 支持写法
function foo(a: i32 = 0): i32 {
  var b = a + 1
  return b
}

不支持union types联合类型

// 不支持写法
function foo(a: f64 | string): void {
}

// 支持写法T,在内部判断
function foo<T>(a: T): void {
  if(isString<T>(a)){

  }else if(isInteger<T>(a)){

  }
}

不支持正则Regex

// 不支持写法
var myRe = new RegExp('d(b+)d', 'g');
var myArray = myRe.exec('cdbbdbsbz');

可以通过第三方库支持:https://www.assemblyscript.org/status.html#regexp

不支持JSON

// 不支持写法
const obj = JSON.parse('{"name":"jack"}');

推荐用waft-json:npm i waft-json
也可以通过第三方库支持:https://github.com/nearprotocol/assemblyscript-json

不含定时器API

可以通过waft框架提供定时器的API使用。

// 不支持
setTimeout(()=>{
},500)
// 不支持
setInterval(()=>{
},500)

不支持?参数省略

// 不支持写法
const foo = (a: string, b?: string):void =>{}
foo('name1');

// 支持写法
const foo = (a: string, b: string = "name2"):void =>{}
foo('name1')

不支持异常处理和try catch

// 不支持写法
function doThrow(): void {
  throw new Error("message")
}
// 不支持写法
try{
  doThrow()
}catch(e){
}

不支持 ===, 支持 ==

// 不支持写法
let path = event.data.getString('path') === 'pages/index/index' ? 'index' : 'wordcase';
// 支持语法
let path = event.data.getString('path') == 'pages/index/index' ? 'index' : 'wordcase';