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.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e455d5-27067.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e455d5-27067.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?