![](/img/trans.png)
[英]Uncaught TypeError: undefined is not a function while creating the object using variable
[英]Uncaught Type error: undefined is not a function while creating an object
Base64.js
var Base64 = {
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
encode: function(e) {
var t = "";
var n, r, i, s, o, u, a;
var f = 0;
e = Base64._utf8_encode(e);
while (f < e.length) {
n = e.charCodeAt(f++);
r = e.charCodeAt(f++);
i = e.charCodeAt(f++);
s = n >> 2;
o = (n & 3) << 4 | r >> 4;
u = (r & 15) << 2 | i >> 6;
a = i & 63;
if (isNaN(r)) {
u = a = 64
} else if (isNaN(i)) {
a = 64
}
t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a)
}
return t
}
}
我從另一個文件調用編碼方法,如下所示已導入了base64.js
文件並創建了對象
Var base64=new Base64.encode(input);
錯誤:
未捕獲的類型錯誤:undefined不是函數
可以幫忙嗎?
Var
應該是var
_utf8_encode
未定義。 var encodedStr = Base64.encode(input)
在這條線
t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a)
在原始代碼中, this
是指new
而不是Base64
創建的對象,因此this._keyStr
是undefined
。 但是,如果直接調用Base64.encode()
(上面的#3),它將引用Base64
並且您將獲得適當的值。
var Base64 = { _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", encode: function(e) { var t = ""; var n, r, i, s, o, u, a; var f = 0; e = Base64._utf8_encode(e); while (f < e.length) { n = e.charCodeAt(f++); r = e.charCodeAt(f++); i = e.charCodeAt(f++); s = n >> 2; o = (n & 3) << 4 | r >> 4; u = (r & 15) << 2 | i >> 6; a = i & 63; if (isNaN(r)) { u = a = 64 } else if (isNaN(i)) { a = 64 } t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a) } return t }, _utf8_encode: function(e) { // do your processing return e } } var input = "Hello World"; var base64 = Base64.encode(input); console.log(base64)
注意:我已經定義了_utf8_encode
,但是它返回相同的文本。 請添加必要的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.