我听说您的匿名函数的名称有助于调试。
JQuery:
$( "p" ).on( "click", function clickHndlr() {
/* body...*/
});节点:
var EventEmitter = require('events').EventEmitter,
emitter = new events.EventEmitter();
emitter.on('customEvent', function customEventHndlr (message, status) {
/* body...*/
});Vanilla JS:
button.addEventListener('keypress', function buttonHndlr() {
/* body...*/
});但是对象呢?
var starShipChecker = (function() {
var publicAPI = {
checkForWarpDrive : function(starShip){
if(!starShip.hasOwnProperty('warpDrive')) {
starShip.warpDrive = undefined;
console.log('Your star-ship, the ' + starShip.name + ', now has warp-drive!' +
'\n' + 'Use the addWarpDrive method to apply the maximum warp relevant to your ship Class...');
} else {
console.log('Your star-ship, the ' + starShip.name + ', has warp-drive already!' +
'\n' + 'But use the addWarpDriveMaxLevel method to apply the maximum warp relevant to your ship Class...');
}
},
addWarpDriveMaxLevel : function(){}
};
return publicAPI;
})();你会得到同样的好处吗?还是因为它们是方法而不同?
checkForWarpDrive : function checkWarpDriveLikeYouWereScotty(starShip){ /* body...*/},
addWarpDriveMaxLevel : function addWarpDriveLikeYouWereScotty(){ /* body...*/}发布于 2016-10-12 21:17:31
是。同样的好处(and more)在那里。
然而,引擎/调试器正变得越来越智能化,并将根据它们所属的object属性的键隐式地命名函数。ES6甚至要求这样做(检查 property)。但是如果您使用的是ES6,那么您可能无论如何都在使用method definition :-)
https://stackoverflow.com/questions/40008445
复制相似问题