The problem of abbreviation of es6 method

var obj1= {

fn1 (){}.bind()

}
var obj2= {

fn2: function(){}.bind()

}

fn1 will report an error, fn2 is normal, what is the principle?

Apr.03,2021

var obj1= {
    fn1 (){}
}

Why did I report an error? Because what you wrote is not correct


abbreviation is only for fn () {} = > fn: function () {} your writing mode engine does not recognize


this syntax error.
es6 defines the shorthand of properties and methods within an object, and the property name of an abbreviated method is always used as a string by the variable itself.

the way you write it above, the bind function itself returns a function. From the perspective of the parser, there is no way to determine the name of the returned function. Like your second way of writing, it can also be customized and correct, so it is impossible to determine that the attribute string cannot be used in this way.

Menu