16

According to ES6 shorthand initialiser, following 2 methods are same:

In ES5

var person = {
  name: "Person",
  greet: function() {
     return "Hello " + this.name;
  }
};

In ES6

var person = {
  name: "Person",
  greet() {
     return "Hello " + this.name;
  }
};

Do the ES6 way is in anyway different from the previous way? If not then using "super" inside them should be also treated as equal, which doesn't hold true, please see below two variaiton:

Below works

let person = {
  greet(){
   super.greet(); 
  }
};

Object.setPrototypeOf(person, {
  greet: function(){ console.log("Prototype method"); }
});

person.greet();

Below fails

let person = {
  greet: function(){
   super.greet(); // Throw error: Uncaught SyntaxError: 'super' keyword unexpected here
  }
};

Object.setPrototypeOf(person, {
  greet: function(){ console.log("Prototype method"); }
});

person.greet();

The only difference in above 2 examples is the way we declare method greet in person object, which should be same. So, why do we get error?

Ratnesh Lal
  • 319
  • 4
  • 6
  • 17

1 Answers1

26

So, why do we get error?

Because super is only valid inside methods. greet: function() {} is a "normal" property/function, not a method, because it doesn't follow the method syntax.

The differences between a method and a normal function definition are:

  • Methods have a "HomeObject" which is what allows them to use super.
  • Methods are not constructable, i.e. they cannot be called with new.
  • The name of a method doesn't become a binding in the method's scope (unlike named function expressions).
Felix Kling
  • 705,106
  • 160
  • 1,004
  • 1,072
  • Regarding "Methods are not constructable, i.e. they cannot be called with new.", I am able to create new object using new : http://www.es6fiddle.net/iskh3nhl/ – Ratnesh Lal Sep 01 '16 at 15:15
  • 2
    es6fiddle seems to compile the code to ES5. If you post the code into the browser console (assuming your browser supports ES6), you will get `Uncaught TypeError: person.greet is not a constructor(…)`. – Felix Kling Sep 01 '16 at 15:28
  • Can you suggest any best book or other learning source for ES6? Which talks about such depth of each new features in ES6? – Ratnesh Lal Sep 01 '16 at 15:40
  • https://github.com/getify/You-Dont-Know-JS is the best I know. Very technical. Not sure if it talks about that though. – Felix Kling Sep 01 '16 at 15:42
  • 4
    @FelixKling In ES6, greet(){} seems to be a shorthand expression for greet: function(){}. So why would the super keyword still work differently between those two? – whales Dec 21 '16 at 04:05
  • https://stackoverflow.com/a/15285702/5238583 But this answer says that `method` is just function value in object ? – Tina Chen Jul 04 '18 at 01:38
  • In [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions), there seems no such definition. Sorry for that I cannot quite understand the Specification. I have the same doubt with @whales – Tina Chen Jul 04 '18 at 01:48
  • @Tina: That answer is from 2013, so before ES2015 was released. Until then, we just informally called functions that are properties of objects "methods". *"In MDN, there seems no such definition."* What do you mean? The page you linked shows exactly what methods are (syntax-wise). I can update the page if that's helpful. A simple way to realize that these two are different is: `var obj = {foo: function() {}, bar() {}}; new obj.foo(); new obj.bar();` One works, the other doesn't. So it's not *just* a shorthand. Does this help? – Felix Kling Jul 04 '18 at 15:57
  • 1
    Thanks for your patience, I've moved my doubt to another [question](https://stackoverflow.com/questions/51165046/why-homeobject-is-different-in-shorthand-syntax-of-method) and got an answer. Now I'm fully understand shorthand syntax(method) is not just a shorter function syntax in ES2015. (And also learned how to read specification...) – Tina Chen Jul 05 '18 at 01:09