DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> JavaScript基礎知識 >> Google Dart編程語法和基本類型學習教程
Google Dart編程語法和基本類型學習教程
編輯:JavaScript基礎知識     

1. 變量聲明

如何定義變量

復制代碼 代碼如下:
var name = 'Bob';

變量的初始值

復制代碼 代碼如下:
int lineCount;
assert(lineCount == null); // Variables (even numbers) are initially null.

可以使用var,也可以直接指定類型。
final, 定義為final的變量,值不能夠被更改

復制代碼 代碼如下:
final name = 'Bob'; // Or: final String name = 'Bob';
name = 'Alice';     // ERROR

2. 基礎類型

字符串

字符串可以使用單引號或者雙引號。
復制代碼 代碼如下:
var s1 = 'Single quotes work well for string literals.';
var s2 = "Double quotes work just as well.";


在字符串中,可以直接應用值, ${表達式}, 如果只是一個變量,就可以去掉{}
復制代碼 代碼如下:
var s = 'string interpolation';
assert('Dart has $s, which is very handy.' ==
       'Dart has string interpolation, which is very handy.');
assert('That deserves all caps. ${s.toUpperCase()} is very handy!' ==
       'That deserves all caps. STRING INTERPOLATION is very handy!');


多行字符串,會被認為默認拼接。
復制代碼 代碼如下:
var s = 'String ''concatenation'
        " works even over line breaks.";
assert(s == 'String concatenation works even over line breaks.');

如果要使用多行字符串,可以這樣, 用'''
復制代碼 代碼如下:
var s1 = '''
You can create
multi-line strings like this one.
''';


創建一個不考慮轉義的字符串
復制代碼 代碼如下:
var s = @"In a raw string, even \n isn't special.";

StringBuffer, 非常類似.net中的。
復制代碼 代碼如下:
var sb = new StringBuffer();

sb.add("Use a StringBuffer ");
sb.addAll(["for ", "efficient ", "string ", "creation "]);
sb.add("if you are ").add("building lots of strings.");

var fullString = sb.toString();

數字

主要就2種,int 和 double, 它們都繼承num類型

數字和字符串之間的轉換

復制代碼 代碼如下:
// String -> int
var one = Math.parseInt("1");
assert(one == 1);

// String -> double
var onePointOne = Math.parseDouble("1.1");
assert(onePointOne == 1.1);

// int -> String
var oneAsString = 1.toString();
assert(oneAsString == "1");

// double -> String
var piAsString = 3.14159.toStringAsFixed(2);
assert(piAsString == "3.14");


布爾類型

bool,不同於js, 只要不是true, 那麼就都是false.

Lists(可以當做數組)

復制代碼 代碼如下:
var list = [1,2,3]; //實例化一個list
list.add(4);       //添加一個元素4

可以使用for, for...in, foreach()來遍歷一個list.
復制代碼 代碼如下:
var list = [1,2,3];
for (final x in list) {
  print(x);
}


或者

復制代碼 代碼如下:
var list = [1,2,3];
list.forEach((element) => print(element));

Maps(字典類型)

復制代碼 代碼如下:
var gifts = {                         // A map literal
// Keys       Values
  "first"  : "partridge",
  "second" : "turtledoves",
  "fifth"  : "golden rings"};

gifts["third"] = "apple"; //添加一個

使用foreach遍歷
復制代碼 代碼如下:
var gifts = {
  "first" : "partridge",
  "second": "turtledoves",
  "fifth" : "golden rings"};
gifts.forEach((k,v) => print('$k : $v'));

getKeys()和getValues()方法
復制代碼 代碼如下:
var gifts = {"first": "partridge", "second": "turtledoves"};
var values = gifts.getValues();

//Print partridge and turtledoves, but not necessarily in that order.
values.forEach((v) => print(v));

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved