简体   繁体   English

Ecmascript 6语法?

[英]Ecmascript 6 syntax?

I am referring to the implementation of android back button for react native but I am not sure how to interpret this snippet code(for function arguments of addEventListener and removeEventListener . Isn't named parameter supposed to be using = sign?. eg 我指的是响应本机的android back button的实现,但我不确定如何解释此代码段代码(对于addEventListenerremoveEventListener函数参数。不是命名参数应该使用=符号吗?例如

var foo = function (a = 1) {}

var BackAndroid = {

  exitApp: function() {
    DeviceEventManager.invokeDefaultBackPressHandler();
  },

  addEventListener: function (
    eventName: BackPressEventName,
    handler: Function
  ): {remove: () => void} {
    _backPressSubscriptions.add(handler);
    return {
      remove: () => BackAndroid.removeEventListener(eventName, handler),
    };
  },

  removeEventListener: function(
    eventName: BackPressEventName,
    handler: Function
  ): void {
    _backPressSubscriptions.delete(handler);
  },

};

What you are seeing are default parameters. 您看到的是默认参数。 In that function it's saying if you don't pass a value for a or pass undefined, instead use 1. 在该功能它说,如果你不为传递值a或通过未定义,改用1。

Default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed. 如果未传递任何值或未定义,则默认函数参数允许使用默认值初始化形式参数。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Default_parameters

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM