Home > Javascript > JavaScript (1)

JavaScript (1)

總結第一周,嗯,是第一至四周所學,因為第二三四周所學不多,多數是溫故知新及提供更多練習讓你熟習Javascript而已:

JavaScript Primitive Data Types:

  • var a = “XXX”;
    //string,以「””」雙引號標示
  • var b = 12.5;
    //number,毋須標示
  • var isOdd = function(n){if (n%2===0){return false;} else {return true;}};
    //boolean,真假值,只有true或false
    //每當有比較運算子就會傳回boolean
  • undefined
  • null(“”)
Ways of Storing Data:
  • var variable = XXX/”XXX”;
  • var array = [X, Y, Z];
    //array,以「[]」中括號標示,其中的各項目以「,」分隔,裏面可以包含numbers、strings、boolean、array和objects
  • var obj = {fuct: function(){}, var:”value”};
    var obj2 = new Object();
    //object,以「{}」大括號標示,其中的各項目以「,」分隔,裏可以有不同的properties,如numbers、strings,甚至methods
    //在object可以任意加設variable或function的properties,例子中obj兩個properties全局名稱為obj.fuct及obj.var,亦可以稱為obj[“fuct”]及obj[“var”],在obj中可以this.fuct及this.val代替

JavaScript Operators:

  • ===:if equal
  • !==:if not equal
  • !:NOT
  • ++:+1
  • –:-1
  • a+=b:a=a+b
  • a-=b:a=a-b
  • ||:OR
  • &&:AND
  • a%b:modulus(a除以b的餘數)
  • a=b>c?d:e;
    //ternary operators:var=condition?result1:result2;
    if (x>y){result=true;}else{result=false}
    可以重寫成:result=x>y?true:false;

JavaScript Common Functions:

  • var XXX = 12.5 ;
    //宣告variable XXX,值為12.5)
  • var XXX = function (parameterA,parameterB) {
    return parameterA+parameterB;
    };
    //宣告function XXX,有a和b兩個arguments或者parameters(參數),計算並傳回a+b
    //return用於「傳回」結果,同時意味function結束
  • typeof XXX
    //顯示格式,傳回”string”/”number”/”object”等
  • isNaN(XXX/”XXX”)
    //檢查是否NaN,傳回true或false

JavaScript Common functions(Printings):

  • console.log(“XXX”);
    //顯示XXX
  • alert(“XXX”);
    //彈出對話方塊XXX
  • confirm(“XXX”);
    //彈出對話方塊XXX並要求選擇Yes或No
  • prompt(“How old are you”);
    var answer = prompt(“How old are you?”);
    //彈出對話方塊「How old are you?」並要求輸入文字,answer會傳回輸入了的文字

JavaScript Common functions(Math):

  • Math.random():傳回0至1的任意數字
  • Math.round(“XXX”)
    //將XXX四寫五入至最接近整數
  • Math.floor(target)
    //將target內的數字round down
  • Math.pow(num1,num2)
    //num1 to ther power num2
  • Math.max(num1,num2)
    //find the largest number among num1 and num2

Build-in properties for strings/arrays:

  • “XXX”.length;
    //傳回一個字串的長度或array的長度
  • “XXX”.substring(1,1);
    //在”XXX”這個string中,輸出從第2個(1)起,到第2個(1)字母,即「X」
    //若未輸入第二個parameter,則會一直輸出直至字串結束
  • “XXX”.toUpperCase()
    //XXX全部轉為大楷
  • “XXX”.toLowerCase()
    //XXX全部轉為小楷
  • “XXX”.indexOf(“target”)
    //在XXX中,找第一個target的位置,第一個字母為0
  • “XXX”.replace(“original”,”new”)
    //在XXX中,尋找original並取代為new,只進行一次

Control Structures:

  • if (condition) { action A }; else{ action B }
  • for ( initialization; condition; increment ) { // code to run each iteration }
  • while (condition) {action A; action B; }
  • do {action A; action B; } while (condition)
  • switch (parameter) { case “XXX”: action A; break; … default: action X; break; }

Reminders:

  • Variables分為global variable及local variable,前者適用範圍(scope)是全局,後者適用範圍僅於function之內。若出現同名的global variable及local variable,function中優先使用local variable,但function以外不會使用local variable
  • 除了for、while這些loop structures外,使用recursion也可以令function有同樣效果
  • Strings、arrays、objects性質相近,strings可以理解為arrays of characters,因此一些arrays用的性質如array.length或array[i]等也可以用在strings上,此外,arrays中可以包括arrays
  • Functions中有「return」可以結束function,loop structures也有「break」可以結束loops
  • typeof.XXX/typeof”XXX”
    //顯示格式,傳回”string”/”number”/”object”等
  • isNaN(XXX/”XXX”)
    //檢查是否NaN,傳回true或false
  • typeof XXX
    //顯示格式,傳回”string”/”number”/”object”等
  • isNaN(XXX/”XXX”)
    //檢查是否NaN,傳回true或false
Categories: Javascript
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: