这篇文章用尽量简短的篇幅来讲解javascript, 但会覆盖所有主要语言特征. javascript很多奇怪的地方, 但也是很有意思的语言.
1. 预备
首先你要学会使用控制台来运行代码示例
按 Ctrl+Shift+J (Windows / Linux) 或者 Cmd+Opt+J (Mac)。
注释:
// a single-line comment (at the end of a line)
/*
A multi-line comment (anywhere)
*/
表达式
console.log("hello"); // print something
var myvar = 7; // declare a variable
2. 数据类型
在javascript里, 你有下面的数据类型
- Boolean 布尔:
true
或者false
- Number 数字:
1
,1.0
... - String 字符串:
"abc" 'abc' \
abc ` ` - Function 函数:
function twice(x) { return 2 * x; } console.log(twice(4)); // 8
- Object 对象: 对象使用属性存放数据, 每个属性有一个
name
和value
var obj = { propName1: 123, propName2: "abc" } obj.propName1 = 456; obj["propName1"] = 456; // same as previous statement
- Array 数组
var arr = [true, "abc", 123]; console.log(arr[1]); // abc console.log(arr.length); // 3
- undefined
var a; console.log(a);// undefined
8. null `最好别用`
... to be done
网友评论