0

I am trying to use pdf.js in an XFA PDF that only supports JavaScript 1.5 and therefore doesn't recognize Object.create() and call(). There are nearly 30 uses of Object.create() in pdf.js.

Is there a way I can add a function to extend Object() to include create()? It's just for the purpose of backwards compatibility.

Kyle

5
  • 1
    "...therefore doesn't recognize Object.create() and call()..." Pretty sure call has been in JavaScript since about 1998. Commented Aug 22, 2014 at 15:31
  • You just have to look at the MDN documentation for a polyfill: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Aug 22, 2014 at 15:34
  • (That's so meta -- to use Adobe Reader to parse PDFs using PDF.js) Commented Aug 22, 2014 at 15:45
  • Yes I know. But using the Acrobat JS API for what I was doing was crashing Adobe Reader 10. Commented Aug 22, 2014 at 15:49
  • possible duplicate of Javascript Object.create not working in Firefox Commented Aug 22, 2014 at 15:54

1 Answer 1

0

Got it:

if(typeof Object.create !== "function") {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}

Thanks for your responses. Ya, I don't know why I included call() in there.

Kyle

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

5 Comments

Note that the real Object.create has a second argument, which cannot be shimmed on pre-ES5 environments. When shimming, I recommend looking to see if you've gotten a second argument and throwing an exception.
In addition to what @T.J.Crowder said, the standard Object.create lets you set null as the prototype, but the typical shim that you've posted will fail when creating the new object. So you may want to test for null, and substitute Object.prototype in its place.
...and actually, since this seems to be for Mozilla's JS, you could probably use __proto__ as the patch, which will accept null if I recall. var new_o = {}; new_o.__proto__ = o; return new_o;
Good catch cookie. I will use that.
T.J. luckily for me pdf.js doesn't use the second argument for Object.create.

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.