把一些比較特別需要記的記錄下來。
- JavaScript 跟 Java 沒關係,雖然 Netscape 想讓他們看起來有關係。 XD
- JavaScript 是一個直譯式、動態型別、弱型別、原型導向的程式語言。
Data Type
Primitive Data Type
- Number
沒有區分 Interger 與 Float point number - String
Single qoute or double qoute are all acceptable. - Boolean
Keyword:true
flalse
- Undefined
- Null
Composite Data Type
- Array
- Object
Can be nested, contain other composite data type within it.
Syntax
Operator
===
: equal operator!==
: not equal operator
Basic Input/Output
console.log()
confirm()
prompt()
Variable Declearation & Definiton
var variableName;
var variableName = value;
Array Declearation & Definiton
var emptyArray = [];
var arrayName = [data, data, data];
var mixed = [34, "candy", "blue", 11];
var nested = [ [1, 2, 3], ["Alice", "Bob"] ];
Object Declearation & Definiton
var myObject = {};
var myObject = new Object();
Object members can be access by useing member name string as inedx.
var myObject = {
name: "Yiguo",
"birthday": "0628",
getName: function() { console.log(name); },
getBirthday: function() { console.log(birthday); }
};
console.log( myObject["name"] );
console.log( myObject.birthday );
myObject.getName();
myObject["getName"]();
Object constructer can be defined just like a function.
function Person(name,age) {
this.name = name;
this.age = age;
}
var bob = new Person("Bob Smith", 30);
var susan = new Person("Susan Jordan", 25);
Function Declearation & Definiton
function myFunction(a, b) {
return a * b;
}
A JavaScript function can also be defined using an expression, and function expression can be stored in a variable. After a function expression has been stored in a variable, the variable can be used as a function.
var x = function (a, b) {return a * b};
var z = x(4, 3);
Control Flow
for in
loop
for in
loop can be used to loop throught the menber of a object (and array).
var array = [1, 2, 3, 4];
var object = { name:"Yiguo", age:24 }
for ( i in array ) { array[i] += 1; }
for ( x in object ) { console.log( object[x] ); }