JavaScript Function Syntax
A Function is much the same as a Procedure or a Subroutine, in other programming languages.
在其他編程語言,函數是大致相同的程序或子程序。
function name(parameter1, parameter2, parameter3) {
code to be executed
}
name | 在JS中,名稱可以省略。而形成無名函數用法。 |
( parameter1, parameter2, … ) | (小括號) 就是運作執行{}中的代碼之意。從物件導向觀點,就是方法。 |
{ code to be executed } | {封閉代碼區塊}在所有語言,是一個獨立運作的範圍。而它於封閉區塊內的變數通常是獨立的。而要注意的是每個語言對其區塊的互動方式會有不同。 通常對外有的上層、同級、下層的主要互動關係。這個關係就是偏向物件模式。 |
return 語句代碼; //個代碼運作結果就把(資料/值/內容)回傳。至於要傳回什麼內容由自己決定。如果沒設return,則代碼運行完就清空了。
Object Definition
//單行
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};//多行
var person = {
firstName:"John",
lastName:"Doe",
age:50,
eyeColor:"blue"
};
屬性名稱:語句代碼 ,
屬性名稱其實就是變數/常數的名稱,名稱就是"字串"。語句代碼(表達式)的結果就成為它的值。
Creating an Array
Using an array literal is the easiest way to create a JavaScript Array.
Syntax:
var array-name = [item1, item2, ...];
Example:
var cars = ["Saab", "Volvo", "BMW"];
JS的陣列似乎是用來"快速"、"簡便"的存取一連串的資料而設計的,所以名稱的定義就省略。這是JS特色也是使用上要注意的。又它是物件,所以不就是一個表單物件嗎?
item這個值的內容,就直接當作是一個表達式、一個語句。
而用來讀取清單內容,就由0.1.2...依序來指定。 <==這個架構一般程式語言亦慣用
陣列名稱.length //結果:計算item的個數 , (.)不就是物件的用法嗎?
所以處理陣列最常用的法式就用循環 for (起始,判斷,增減值)
陣列是一個物件,typeof 陳列; //結果:是物件。
這就很明白的說明array物件的關係了嗎?
JavaScript Array Methods <==物件導向思維!
The strength of JavaScript arrays lies in the array methods.
(陣列最大的價值就是在其(方法)。
PHP也是如此重要
Converting Arrays to Strings
把內容值以文串寫出來
valueOf()、toString()、join(“連接字串”) <==預設值[,]
In JavaScript, all objects have the valueOf() and toString() methods.
JS中的所有物件都有valueOf()及toString()這兩個方法。
For JavaScript arrays, valueOf() and toString() are equal.
就數組而這兩個()方法是相等。
Popping碰!拍!掉最後一個 and Pushing從後推/塞入一個
pop()、push()
When you work with arrays, it is easy to remove elements and add new elements.
This is what popping and pushing is:
Popping items out of an array, or pushing items into an array.
Shifting Elements(移位)
shift() :前進一位,自然擠掉第一個值。
unshift( 結果值) :後退一位,最前補入結果值。
Deleting Elements(刪除某欄的值)
delete oneArrayName[ n ]
刪除[n號欄位]值,仍保留欄位空間。但因為其欄位沒有值,因而資料類型屬"未定義"。
Splicing an Array(併接數組內容)
splice (從此欄,移出幾欄,再由此加 欄1值,欄2值,..)
Sorting an Array(排序內容)
sort() //正向排序
reverse() //反向排序
//內容排序有一定的規則,通常以"字串"型式ACSII的編號決定。所以當是數字值,就會出現不如預期的結果。
The Compare Function
比較函數:function(a, b){return a-b}
Example a-b 小到大 b-a 大到小
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a-b});
Find the Highest (or Lowest) Value
找最大或最小值,利用排序,再讀出首或尾欄值
陣列中連接多個數組
陣列A.concat( A數組,B數組,…) //自己都可以再加入自己的欄值
Array Object
The Array object is used to store multiple values in a single variable:
var cars = ["Saab", "Volvo", "BMW"];
Array indexes are zero-based: The first element in the array is 0, the second is 1, and so on.
For a tutorial about Arrays, read our JavaScript Array Tutorial.
Array Properties
Property
Description
constructor
Returns the function that created the Array object's prototype
length
Sets or returns the number of elements in an array
prototype
Allows you to add properties and methods to an Array object
Array Methods
Method
Description
concat()
Joins two or more arrays, and returns a copy of the joined arrays
indexOf()
Search the array for an element and returns its position
join()
Joins all elements of an array into a string
lastIndexOf()
Search the array for an element, starting at the end, and returns its position
pop()
Removes the last element of an array, and returns that element
push()
Adds new elements to the end of an array, and returns the new length
reverse()
Reverses the order of the elements in an array
shift()
Removes the first element of an array, and returns that element
slice()
Selects a part of an array, and returns the new array
sort()
Sorts the elements of an array
splice()
Adds/Removes elements from an array
toString()
Converts an array to a string, and returns the result
unshift()
Adds new elements to the beginning of an array, and returns the new length
valueOf()
Returns the primitive value of an array
http://www.w3schools.com/js/js_array_methods.asp
詳細內容可以參考:
- w3school.com (http://www.w3schools.com/)
-
程式語言教學誌(http://pydoing.blogspot.tw/)
沒有留言:
張貼留言