0

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
  }
}

I am calling encode method from another file like below had imported base64.js file and created object

Var base64=new Base64.encode(input);

Error:

Uncaught Type error: undefined is not a function

Can any one help?

4
  • 1
    it should be var and not Var Commented Feb 24, 2017 at 9:43
  • Just a random guess, but how about var base64 = new Base64().encode(input)....? Commented Feb 24, 2017 at 9:46
  • Another Base64._utf8_encode(e); but _utf_encode is not defined Commented Feb 24, 2017 at 9:49
  • My intention here is wanted to call encode method from another Js file. Commented Feb 24, 2017 at 9:51

1 Answer 1

1

Errors in your code:

  1. Var should be var
  2. _utf8_encode is not defined.
  3. You are returning encoded string. You do not need to create object. Just do var encodedStr = Base64.encode(input)
  4. In this line

    t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a)
    

    in your original code, this refers to the object created by new, not Base64, so this._keyStr is undefined. But if you directly call Base64.encode() (#3 above), it will refer to Base64 and you will get proper values.

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)

Rectified JSFiddle

Note: I have defined _utf8_encode, but it returns same text. Please add necessary code.

Sign up to request clarification or add additional context in comments.

5 Comments

Got me again (on this). :-) My eyes are unhappy today. I was able to sneak in a ninja above, though. ;-)
Even I updated similar, but your version is better. Thanks :-)
Yeah, sorry, I didn't get the "conflicting edit" notification. We were one second apart! :-)
Please let me know how I can call encode method from another Js file.....I have added all necessary imports like <script src="/base64.js> tag but when I am trying to call encode method getting error
@MadhuNali try logging Base64 in console. You will get entire thing. Then just do var output = Base64.encode(input)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.