javascript - class.prototype.foo=function foo 这样写的用意是什么

查看:123
本文介绍了javascript - class.prototype.foo=function foo 这样写的用意是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

在nodejs的源码中看到这样的写法class.prototype.foo=function foo,想问问这样的的用意是什么?

EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
  if (typeof n !== 'number' || n < 0 || isNaN(n))
    throw new TypeError('n must be a positive number');
  this._maxListeners = n;
  return this;
};

我的问题是问什么还要在function后面再声明一次函数名

// 为什么不这样写,而要在多声明一次
EventEmitter.prototype.setMaxListeners = function (n) {
  if (typeof n !== 'number' || n < 0 || isNaN(n))
    throw new TypeError('n must be a positive number');
  this._maxListeners = n;
  return this;
};

解决方案

我所能想到的就是,在调试的时候,调用栈(Call Stack)中会显示setMaxListeners而非(anonymous function),方便调试吧

这篇关于javascript - class.prototype.foo=function foo 这样写的用意是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆