2

I am a big fan of the constructor/prototype pattern when writing JavaScript. Some time ago, I was told that there are some compatiblity isssues with older browsers (IE7, 8 on XP), but I could not find a ressource to verfiy this claim.

So, my question is: Are there any known compatibility issues when using constructor functions / prototypes in JavaScript? Extra: Could these potential problems be avoided by using the module pattern?

lupor
  • 138
  • 7
  • Who told you that? Did you read it somewhere, can you link where it is stated? – Bergi Jan 20 '15 at 19:02
  • As long as your code is targeting ECMAScript 3, you're good to go. prototypical inheritance was built in to the language since very early days. – u.k Jan 20 '15 at 19:02
  • if you add prototype methods onto existing constructors, especially host objects, problems can ensue. – dandavis Jan 20 '15 at 19:07
  • We had a problem with some old browsers and a coworker told me, it was because of prototypes. After he rewrote it to use the module pattern, it worked (according to him). – lupor Jan 20 '15 at 19:07
  • 1
    @lupor: It would help if you could post the rewrite. – Bergi Jan 20 '15 at 19:28
  • Sorry, can't. Proprietary code. But it was at least partly sloppy, so that could also have been the cause for the errors. Since noone seems to know any serious imcompatiblities, I assume it's alright to use prototype pattern. – lupor Jan 20 '15 at 19:40
  • It's not the question you asked, but there are incompatibilities if you mix prototypes and module pattern unless you do it exactly right. It depends on the Module Pattern implementation, but the Revealing Module Pattern is the worst when it comes to compatibility with prototypes (despite the fact there are proponents out there for "Revealing Prototype Pattern"). – I-Lin Kuo Jan 20 '15 at 22:19
  • Maybe you needed to polyfil Object.create (as the answer suggest) but without any code I don't know. Here is the constructor pattern with inheritance and mix ins: http://stackoverflow.com/a/16063711/1641941 – HMR Jan 21 '15 at 00:59

1 Answers1

1

Are there any known compatibility issues when using constructor functions / prototypes in JavaScript?

No. However, "class" inheritance should be done using Object.create, which requires a shim for old IEs.

Could these potential problems be avoided by using the module pattern?

No.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375