3

I had some issues to get a website working in Internet Explorer (IE9) and I found that we have defined a function in a javascript object in the following way:

var test = {
    a : function(){alert(123);},
    blafasl(){$("#test").text("456");},
    b : function(){alert(678);},
}

test.blafasl();
//test.b();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='test'></div>

In Chrome and Firefox the code is working but in IE9 it produces an error message.

Is the definition of 'blafasl' valid (and how does it work)? Or is it correct that the IE comes up with an error?

It works in Chrome, Firefox and IE when I change the blafasl definition to: blafasl: function(){....},

I found this: http://www.bryanbraun.com/2014/11/27/every-possible-way-to-define-a-javascript-function but it does not cover my Question...

Greetings Martin

PS: Here is a fiddel of the above code: http://jsfiddle.net/zjobwd89/2/

1 Answers1

-1

It's ES6 object literals : https://github.com/lukehoban/es6features#enhanced-object-literals

You can write it like this :

var obj = {
    // __proto__
    __proto__: theProtoObj,
    // Shorthand for ‘handler: handler’
    handler,
    // Methods
    toString() {
     // Super calls
     return "d " + super.toString();
    },
    // Computed (dynamic) property names
    [ 'prop_' + (() => 42)() ]: 42
};
Arnaud Gueras
  • 2,014
  • 11
  • 14