简体   繁体   English

函数调用后的花括号中的代码块

[英]code block in curly braces after the function call

I'm looking over this code (stripped down version of the implementation of createStore within the Redux source code) 我正在查看这段代码(在Redux源代码中删除了createStore实现的版本)

function createStore(reducer) {
    var state;
    var listeners = []

    function getState() {
        return state
    }

    function subscribe(listener) {
        listeners.push(listener)

        return unsubscribe() {
            var index = listeners.indexOf(listener)
            listeners.splice(index, 1)
        }
    }

    function dispatch(action) {
        state = reducer(state, action)
        listeners.forEach(listener => listener())
    }

    dispatch({})

    return { dispatch, subscribe, getState }
}

My question is specific to the below block inside 我的问题是针对下面的内部块

function subscribe(listener)


return unsubscribe() {
   var index = listeners.indexOf(listener)
   listeners.splice(index, 1)
}

The section in the curly bracket 花括号中的部分

{
   var index = listeners.indexOf(listener)
   listeners.splice(index, 1)
}

does that block gets passed to the unsubscribe() method? 该块是否传递给unsubscribe()方法? any resemblance to Ruby's blocks? 与Ruby的块有什么相似之处? how does that work in javascript? 这在javascript中如何工作?

Whan you call subscribe and pass it a listener, it returns you a function which you can call anytime later. 你调用subscribe并传递给一个监听器,它会返回一个你可以在以后随时调用的函数。 Ex: 例如:

const {dispatch, subscribe, getState} = createStore(this.myReducer); 
// for demonstration purpose. Now you have references to the values createStore() returns;

const subscription = this.subscribe(this.listener)

Now whenever you call this.subscription() it will execute this: 现在每当你调用this.subscription()它都会执行:

var index = listeners.indexOf(listener)
listeners.splice(index, 1)

with listener being saved in a closure. listener保存在闭包中。

First of all, there are some issues with the code you posted. 首先,您发布的代码存在一些问题。 A better stripped-down version of the createStore() function would be: createStore()函数的更好的精简版本将是:

function createStore(reducer) {
  var listeners = [];

  function subscribe(listener) {
    listeners.push(listener);

    return function unsubscribe() {
      var index = listeners.indexOf(listener)
      listeners.splice(index, 1)
    };
  }

  return {subscribe};
}

Note that you were forgetting some semicolons and that ( EDIT: the OP forgot nothing. Redux skips semicolons to conform to React Router ESLint ) the subscribe() method wasn't returning function unsubscribe() but just unsubscribe() . 请注意, 您忘记了一些分号编辑:OP忘记了什么.Redux 跳过分号以符合 React Router ESLintsubscribe()方法没有返回function unsubscribe()但只是unsubscribe()

Now, answering the question, this article is a nice lecture to illustrate the differences between Ruby and JavaScript on this topic. 现在,回答这个问题, 本文是一个很好的讲座,用于说明Ruby和JavaScript在这个主题上的区别。

Ruby methods are not functions or first-class citizens because they cannot be passed to other methods as arguments, returned by other methods, or assigned to variables. Ruby方法不是函数或一等公民,因为它们不能作为参数传递给其他方法,由其他方法返回或分配给变量。 Ruby procs are first-class, similar to JavaScript's first-class functions. Ruby procs是一流的,类似于JavaScript的一流函数。

In JavaScript functions are truly first-class citizens. 在JavaScript中,函数是真正的一流公民。 They can be passed a round as any other piece of data. 它们可以作为任何其他数据传递一轮。

In our example, the createStore() function returns an object, containing the function/method subscribe() . 在我们的示例中, createStore()函数返回一个对象,其中包含函数/方法subscribe() It does so by returning the name of the function ( subscribe ). 它通过返回函数的名称( subscribe )来实现。 Likewise, subscribe() also returns a function, but this time the declaration of that function happens directly inside the return statement. 同样, subscribe()也返回一个函数,但这次函数的声明直接在return语句中发生。 Both are valid ways to pass a function. 两者都是传递函数的有效方法。

When you instantiate createStore by a function call, you will obtain the returned object. 通过函数调用实例化createStore ,您将获得返回的对象。

var myObject = createStore("foo");

The new object has the method subscribe() . 新对象的方法为subscribe() If you call that method you will obtain the unsubscribe() function. 如果您调用该方法,您将获得unsubscribe()函数。

var myFunction = myObject.subscribe("bar");

Of course, you could do it in one line by: 当然,您可以通过以下方式在一行中完成:

var myFunction = createStore("foo").subscribe("bar");

Try it in the snippet below: 在下面的代码中尝试一下:

 function createStore(reducer) { var listeners = []; function subscribe(listener) { listeners.push(listener); return function unsubscribe() { var index = listeners.indexOf(listener) listeners.splice(index, 1) }; } return {subscribe}; } var myObject = createStore("foo"); console.log(myObject); // print an object with the subscribe method. var myFunction = myObject.subscribe("bar"); console.log(myFunction); // print the unsubscribe function console.log(createStore("foo").subscribe("bar")); 

You might also want to read about objects in MDN . 您可能还想阅读MDN中的对象。

该代码块是subscribe方法返回的unsubscribe函数的一部分

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

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