0

new to javascript: Internet Explorer 8 does not support object.create(), here is an example:

var tilo = Object.create(Person); 

ok, so IE does not support it. What should I do next? Should I create 2 different javascript files.. one for Firefox, one for IE ?

fish man
  • 101
  • 1
  • 7
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create @fishman : one protip consist of looking for shim (this is the important word) when something is not implemented by IE or any other browser – dievardump Jul 19 '13 at 21:14
  • @fishman http://en.wikipedia.org/wiki/Shim_(computing) AKA "polyfill" - http://en.wikipedia.org/wiki/Polyfill – Ian Jul 19 '13 at 21:19

1 Answers1

6

From MDN's docs, use this:

if (!Object.create) {
    Object.create = (function () {
        var F = function(){};

        return function (o) {
            if (arguments.length !== 1) {
                throw new Error('Object.create implementation only accepts one parameter.');
            }
            F.prototype = o;
            return new F();
        };
    }());
}

Include this on your page before you attempt to use Object.create. It detects if it's natively available; if it's not, it makes it available by using this custom code. This should technically make it available in any browser.

You should never make script files for specific browsers; browser versions differ in their feature support...that's why you should always use feature detection (like this code). Internet Explorer 9 supports this, so you shouldn't generalize that IE needs it.

Reference:

Community
  • 1
  • 1
Ian
  • 50,146
  • 13
  • 101
  • 111
  • i dont understand this code, are you returning a function from a function? would you be able to explain this a bit more ? – fish man Jul 22 '13 at 20:19
  • @fishman Well, it's not my code; it's MDN's. But I'll try to explain. Obviously it checks to see that `Object.create` doesn't exist first. If it doesn't, then it defines it. The value is wrapped in `(function () { }());` which creates a closure (executing the outer function immediately, which returns the inner function). The point of that is to execute `var F = function(){};` once, and reuse `F` every time `Object.create` is called. The value of `Object.create` ends up being the innermost function. – Ian Jul 22 '13 at 20:25