2020年3月25日 星期三

從 dart 呼叫 javascript


參考官網文章:  http://dartdoc.takyam.com/articles/js-dart-interop/


// index.html

<html><head><meta charset='utf-8'></head><body>
    <script type='application/javascript' src='hello.js'></script>
    <script type='application/javascript' src='main.js'></script>
</body></html>

// hello.js
var greeting="Hello"; // 1. variable

function fun( ){ //2. function
    console.log("How are you?");
}

class Rectangle {
    constructor(width, height) {
      this.width = width;
      this.height = height;
    }
}
function rectangle(width, height) { // 3. object
  return new Rectangle(width, height); // return an object
}

// main.dart
import 'dart:js';
void main() { 

  var greeting = context['greeting']; // 1.
  print("$greeting");

  context.callMethod('fun'); //2.

  final rectangle = JsObject(context['rectangle'], [5, 1]); //3.
  print(rectangle['width']);
}

沒有留言:

張貼留言

在 linux 系統下簡單的 tar 檔案讀寫程式

參考網站: https://github.com/calccrypto/tar/tree/master,  改寫成我想用的: listtar.cpp #include <stdio.h> #include <stdlib.h> #include <s...