- 下載 eclipse
- Help→Software Updates→Available Software 中加入 Europa Discovery Site 的下載網址: http://download.eclipse.org/releases/europa/
- 如下圖,在 Other Tools 中選取安裝 Eclipse Monkey
- 安裝完後,重新啟動 eclipse,應該會在選單列中看到一個新的選單:Scripts
2008年3月21日 星期五
在 eclipse 中撰寫 script,並利用 script 控制 eclipse platform
於
下午5:12
2008年1月3日 星期四
JavaScript 的 Hashtable 實作
JavaScript 的 Hashtable 實作
/*******************************************************************************************
* Object: Hashtable
* Description: Implementation of hashtable
* Author: Uzi Refaeli
*******************************************************************************************/
//======================================= Properties ========================================
Hashtable.prototype.hash = null;
Hashtable.prototype.keys = null;
Hashtable.prototype.location = null;
/**
* Hashtable - Constructor
* Create a new Hashtable object.
*/
function Hashtable(){
this.hash = new Array();
this.keys = new Array();
this.location = 0;
}
Hashtable.prototype.keySet = function(){
return this.keys;
}
/**
* put
* Add new key
* param: key - String, key name
* param: value - Object, the object to insert
*/
Hashtable.prototype.put = function (key, value){
if (value == null)
return;
if (this.hash[key] == null)
this.keys[this.keys.length] = key;
this.hash[key] = value;
}
/**
* get
* Return an element
* param: key - String, key name
* Return: object - The requested object
*/
Hashtable.prototype.get = function (key){
return this.hash[key];
}
/**
* remove
* Remove an element
* param: key - String, key name
*/
Hashtable.prototype.remove = function (key){
for (var i = 0; i < this.keys.length; i++){
//did we found our key?
if (key == this.keys[i]){
//remove it from the hash
this.hash[this.keys[i]] = null;
//and throw away the key...
this.keys.splice(i ,1);
return;
}
}
}
/**
* size
* Return: Number of elements in the hashtable
*/
Hashtable.prototype.size = function (){
return this.keys.length;
}
/**
* populateItems
* Deprecated
*/
Hashtable.prototype.populateItems = function (){}
/**
* next
* Return: true if theres more items
*/
Hashtable.prototype.next = function (){
if (++this.location < this.keys.length)
return true;
else
return false;
}
/**
* moveFirst
* Move to the first item.
*/
Hashtable.prototype.moveFirst = function (){
try {
this.location = -1;
} catch(e) {/*//do nothing here :-)*/}
}
/**
* moveLast
* Move to the last item.
*/
Hashtable.prototype.moveLast = function (){
try {
this.location = this.keys.length - 1;
} catch(e) {/*//do nothing here :-)*/}
}
/**
* getKey
* Return: The value of item in the hash
*/
Hashtable.prototype.getKey = function (){
try {
return this.keys[this.location];
} catch(e) {
return null;
}
}
/**
* getValue
* Return: The value of item in the hash
*/
Hashtable.prototype.getValue = function (){
try {
return this.hash[this.keys[this.location]];
} catch(e) {
return null;
}
}
/**
* getKey
* Return: The first key contains the given value, or null if not found
*/
Hashtable.prototype.getKeyOfValue = function (value){
for (var i = 0; i < this.keys.length; i++)
if (this.hash[this.keys[i]] == value)
return this.keys[i]
return null;
}
/**
* toString
* Returns a string representation of this Hashtable object in the form of a set of entries,
* enclosed in braces and separated by the ASCII characters ", " (comma and space).
* Each entry is rendered as the key, an equals sign =, and the associated element,
* where the toString method is used to convert the key and element to strings.
* Return: a string representation of this hashtable.
*/
Hashtable.prototype.toString = function (){
try {
var s = new Array(this.keys.length);
s[s.length] = "{";
for (var i = 0; i < this.keys.length; i++){
s[s.length] = this.keys[i];
s[s.length] = "=";
var v = this.hash[this.keys[i]];
if (v)
s[s.length] = v.toString();
else
s[s.length] = "null";
if (i != this.keys.length-1)
s[s.length] = ", ";
}
} catch(e) {
//do nothing here :-)
}finally{
s[s.length] = "}";
}
return s.join("");
}
/**
* add
* Concatanates hashtable to another hashtable.
*/
Hashtable.prototype.add = function(ht){
try {
ht.moveFirst();
while(ht.next()){
var key = ht.getKey();
//put the new value in both cases (exists or not).
this.hash[key] = ht.getValue();
//but if it is a new key also increase the key set
if (this.get(key) != null){
this.keys[this.keys.length] = key;
}
}
} catch(e) {
//do nothing here :-)
} finally {
return this;
}
};
於
下午5:28
2007年5月31日 星期四
2007年4月13日 星期五
變數的宣告
JavaScript 的區域性變數通常是在函數中宣告,必須使用 var 宣告之;全域性變數則無須使用 var 宣告。
- 區域性變數僅於所在函數中有效。
- 全域性變數於該文件的所有 <script> 標籤中均有效。
範例:
<script>
var arms = 2 //arms是全域變數
legs = 2 //legs是全域變數
function init() {
var heads = 1 //heads是區域變數
eyes = heads * 2 //eyes是全域變數
}
init()
document.write("手臂有",arms,"隻<BR>")
document.write("腿有"+legs+"條<BR>")
document.write("頭有",heads,"個<BR>")
document.write("眼睛有"+eyes+"粒<BR>")
</script>
上例只會印出:
手臂有2隻 腿有2條
因為當執行到「頭有 1 個」的時候,JavaScript 會發生錯誤,而無法繼續執行下去!
- 在函數外宣告的變數,無論是否使用保留字 var 宣告,均稱為全域變數。
- 在函數內宣告的變數,如使用 var 宣告,則為區域變數;未使用 var 宣告,則仍視為全域變數。
function 定義
function 為結構化程式設計的基礎,定義的 function 代表一段副程式。利用關鍵字 function 宣告函式名稱。
function 函式名稱(引數)
{敘述}
若有多個引數(argument),須用逗號隔開。引數的個數可以用以下變數來取得:
函數名稱.arguments.length
引數的值可以用以下變數取得:
函數名稱.arguments[x]
訂閱:
文章 (Atom)