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

dcidev
  • 1
  • 1
  • 1
    *"...therefore doesn't recognize Object.create() and call()..."* Pretty sure `call` has been in JavaScript since about 1998. – T.J. Crowder Aug 22 '14 at 15:31
  • You just have to look at the MDN documentation for a polyfill: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create#Polyfill – Felix Kling Aug 22 '14 at 15:34
  • (That's so meta -- to use Adobe Reader to parse PDFs using PDF.js) – async5 Aug 22 '14 at 15:45
  • Yes I know. But using the Acrobat JS API for what I was doing was crashing Adobe Reader 10. – dcidev Aug 22 '14 at 15:49
  • possible duplicate of [Javascript Object.create not working in Firefox](http://stackoverflow.com/questions/5199126/javascript-object-create-not-working-in-firefox) – cookie monster Aug 22 '14 at 15:54

1 Answers1

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

dcidev
  • 1
  • 1
  • 1
    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. – T.J. Crowder Aug 22 '14 at 15:53
  • 1
    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. – cookie monster Aug 22 '14 at 15:57
  • ...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;` – cookie monster Aug 22 '14 at 15:58
  • Good catch cookie. I will use that. – dcidev Aug 22 '14 at 16:01
  • T.J. luckily for me pdf.js doesn't use the second argument for Object.create. – dcidev Aug 22 '14 at 16:02