2

I stumbled upon this by accident when writing some code:

var obj = {
  myFunc() {
    document.body.innerHTML = 'Hello World!';
  }
};

obj.myFunc();

What I meant to type was this:

var obj = {
  myFunc: function() { 
    // ...
  }
};

I didn't notice I accidentally typed the first one until I realized my code wasn't running in IE11.

Why does the first example work in Chrome/Firefox, and not IE11?

Also, if this is an official language feature, what is this called?

6
  • Thanks, but add it as an answer. Commented Jan 6, 2017 at 20:01
  • 1
    "Why does the first example work in Chrome/Firefox, and not IE11?" — Because only two of those are modern browsers. Commented Jan 6, 2017 at 20:02
  • It is modern ECMAScript which is valid, but only on browsers that can parse it. Commented Jan 6, 2017 at 20:04
  • @Quentin : Isn't IE11 modern browser? Commented Jan 6, 2017 at 20:04
  • @LolCoder아카쉬 — No. It's the lumbering final form of a dead browser that was superseded by Edge almost two years ago. Commented Jan 6, 2017 at 20:05

1 Answer 1

7

It's an Enhanced Object Literal, which is es6 syntax. More formally, Object Initializers allow Method Definitions instead of only key/value pairs as in previous versions.

This table tells the compatibility story under 'object literal extensions'. The short answer is just that IE11 hasn't implemented that feature.

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

Comments

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.