JavaScript 入門指南 - encrypt.js




encrypt.js 的程式原始碼如下

function Encrypt() {
    this.code = "abcdefghijklmnopqrstuvwxyz".split("", 26);
    var i = this.code.length;
    while (i) {
        var j = parseInt(Math.random() * i);
        var x = this.code[--i];
        this.code[i] = this.code[j];
        this.code[j] = x;
    }
    
    this.alph = "abcdefghijklmnopqrstuvwxyz".split("", 26);
}

Encrypt.prototype.showCode = function() {
    return this.code.join("");
}

Encrypt.prototype.setCode = function(c) {
    this.code = c.split("", 26);
}

Encrypt.prototype.toEncode = function(str) {
    var result = "";
    var i = 0;
    var len = str.length;
    while (i < len) {
        var regExp = /^[a-z]+$/;
        if (regExp.test(str[i])) {
            var j = this.alph.indexOf(str[i])
            result += this.code[j];
        }
        else {
            result += str[i];
        }
            
        i++;
    }
    
    return result;
}

Encrypt.prototype.toDecode = function(str) {
    var result = "";
    var i = 0;
    var len = str.length;
    while (i < len) {
        var regExp = /^[a-z]+$/;
        if (regExp.test(str[i])) {
            var j = this.code.indexOf(str[i]);
            result += this.alph[j];
        }
        else {
            result += str[i];
        }
            
        i++;
    }
    
    return result;
}

/* 《程式語言教學誌》的範例程式
   http://pydoing.blogspot.com/
   檔名:encrypt.js
   功能:示範 JavaScript 程式
   作者:張凱慶
   時間:西元 2012 年 12 月 */


您可以繼續參考
範例程式碼


相關目錄
回 JavaScript 入門指南
回 JavaScript 教材
回首頁


參考資料
http://www.ecma-international.org/publications/standards/Ecma-262.htm
http://www.w3schools.com/JS/default.asp
http://www.tutorialspoint.com/javascript/index.htm
http://www.w3.org/standards/techs/dom#w3c_all
https://developer.mozilla.org/en-US/docs/JavaScript
https://developer.mozilla.org/en/DOM
http://msdn.microsoft.com/en-us/ie/default
http://developer.apple.com/devcenter/safari/index.action
http://dev.chromium.org/
http://dev.opera.com/

沒有留言: