2014-07-13 37 views
0

我發現這裏給出在GitHub上一個javascript:https://gist.github.com/faisalman/4213592如何使用這個javascript函數?

(function(){ 
var convertBase = function (num) { 
    this.from = function (baseFrom) { 
     this.to = function (baseTo) { 
      return parseInt(num, baseFrom).toString(baseTo); 
     }; 
     return this; 
    }; 
    return this; 
}; 

// binary to decimal 
this.bin2dec = function (num) { 
    return convertBase(num).from(2).to(10); 
}; 

// binary to hexadecimal 
this.bin2hex = function (num) { 
    return convertBase(num).from(2).to(16); 
}; 

// decimal to binary 
this.dec2bin = function (num) { 
    return convertBase(num).from(10).to(2); 
}; 

// decimal to hexadecimal 
this.dec2hex = function (num) { 
    return convertBase(num).from(10).to(16); 
}; 

// hexadecimal to binary 
this.hex2bin = function (num) { 
    return convertBase(num).from(16).to(2); 
}; 

// hexadecimal to decimal 
this.hex2dec = function (num) { 
    return convertBase(num).from(16).to(10); 
}; 

return this;   
})(); 

和HTML部分我試圖通過使用BIN2HEX function.I轉換給二進制輸入六想要的,如果用戶輸入一個二進制值textarea中給出的文本,通過點擊按鈕它將被轉換爲十六進制值。

似乎沒有發生。我在這裏做什麼錯了?請原諒我,如果我似乎是新手。我已經花了幾個小時在這裏,到目前爲止,已經是jsfiddle了。 http://jsfiddle.net/7BRwL/

+0

甲形式不具有價值。也許你的意思是「this.tal.value = bin2hex(this.form.tal.value)」 – mplungjan

+0

如果函數在最後返回值,我可以在這裏採用什麼其他方法, – user2705577

+1

所有對'convertBase'的調用都顯示爲在前面缺少'新'。否則,2個全局函數'from'和'to'將在每次調用時不斷被破壞。 –

回答

1

您必須設置返回的值。

我將你的onclick屬性複製到一個函數中。這讀取當前輸入,將其轉換並將結果寫入textarea。

document.querySelector('input').onclick = function() { 
    var input = document.querySelector('textarea#ta1'); 
    input.value = bin2hex(input.value); 
}; 

http://jsfiddle.net/7BRwL/6/

所以二進制0101010正確轉換爲十六進制2A

0

來自Github的片段是IIFE(立即調用函數表達式),這意味着它立即執行。該函數返回自身,但由於您尚未將其分配給變量,它會將其自身返回到全局範圍(window.bin2hex()或僅bin2hex())。

所以,你可以將功能簡單地分配給一個變量,如果你喜歡:

var convert = (function(){ ... })(); 
convert.bin2hex(100011101); // will return the hexadecimal equivalent 

見小提琴這裏:http://jsfiddle.net/7BRwL/5/

在你的情況,textarea的的ID是唯一的錯誤,雖然,能簡單地糾正這樣的:

onclick="document.getElementById('ta1').value=bin2hex(document.getElementById('ta1').value) 
+0

'convert === window'如何幫助這個原因? – Bergi

+0

@Bergi它沒有,我只是提供額外的背景信息,關於IIFE的可以如何和經常使用,因爲我從提問者可能不知道這一點的問題中理解。 – Tyblitz

0

更改按鈕,在小提琴到:

<input onclick="convert.ta1.value=bin2hex(convert.ta1.value)" type="button" value="convert"/> 

您正在使用錯誤的名稱爲textarea字段。並且要轉換的值必須作爲參數傳遞給函數。

Updated fiddle

0

如果你只是希望有一個用戶都有自己的文本轉換並顯示,這裏是一個例子,我有。

enter image description here

<body> 
<script> 
//http://stackoverflow.com/questions/2803145/is-there-0b-or-something-similar-to-represent-a-binary-number-in-javascript 
function showConvert(){ 
var userText = document.getElementById('text1').value; 
document.getElementById('text2').value = Number(userText).toString(2); 
document.getElementById('text3').value = Number(userText).toString(16); 
} 
</script> 

<form> 
<center> 
<input id="text1" type="text" value = "47"><br> 
<input id="submit1" type="button" value="Convert" onClick="showConvert();"><br> 
<input id="text2" type="text"><br> 
<input id="text3" type="text"><br> 
</form> 
</body>