JavaScript中的“_”。(插入函数)做什么? (反应的例子)

时间:2017-10-16 01:04:54

标签: javascript reactjs

我仍然很擅长构建React / Redux应用程序。我一直在构建应用程序并查看其他应用程序的代码以学习最佳实践。

查看某人的Reducer,许多案例陈述都是这样的:

case RECEIVE_CURRENT_TEAM:
  return _.merge({}, state, {current_team: action.team})

我想知道_。在合并之前。

我在减速机中编写上述内容的方法如下:

case RECEIVE_CURRENT_TEAM:
  return merge({}, state, {current_team: action.team})

我想知道如果有_的好处。因为我正在建立我的下一个应用程序的最开始。

我发现了一些信息,但是在JavaScript中使用_扩展的文档是模糊且链接的,但链接已被破坏。

http://documentcloud.github.io/underscore/#extend

感谢您的帮助!

3 个答案:

答案 0 :(得分:7)

_不是Javascript功能。这是一个常用的图书馆。

它可以是对lodashunderscore的引用。

这两个库都有很好的文档,因此您可以查找每个函数的功能。

答案 1 :(得分:3)

其他人提到// Include the namespace required to use Unity UI using UnityEngine.UI; public class PlayerController : MonoBehaviour { // Create public variables for player speed, and for the Text UI game objects public float speed; public Text countText; public Text winText; public Text movesText; // Create private references to the rigidbody component on the player, and the count of pick up objects picked up so far private Rigidbody rb; private int count; private int moves; private float location; // At the start of the game.. void Start() { // Assign the Rigidbody component to our private rb variable rb = GetComponent<Rigidbody>(); // Set the count to zero count = 0; // Set moves to zero moves = 0; // Run the SetCountText function to update the UI (see below) SetCountText(); // Set the text property of our Win Text UI to an empty string, making the 'You Win' (game over message) blank winText.text = ""; location = transform.location; SetMovesText(); } void LateUpdate() { if (location != transform.location) { moves = moves + 1; location = transform.location; SetMovesText(); } } // Each physics step.. void FixedUpdate() { // Set some local float variables equal to the value of our Horizontal and Vertical Inputs float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); // Create a Vector3 variable, and assign X and Z to feature our horizontal and vertical float variables above Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); // Add a physical force to our Player rigidbody using our 'movement' Vector3 above, // multiplying it by 'speed' - our public player speed that appears in the inspector rb.AddForce(movement * speed); } // When this game object intersects a collider with 'is trigger' checked, // store a reference to that collider in a variable named 'other'.. void OnTriggerEnter(Collider other) { // ..and if the game object we intersect has the tag 'Pick Up' assigned to it.. if (other.gameObject.CompareTag("Pick Up")) { // Make the other game object (the pick up) inactive, to make it disappear other.gameObject.SetActive(false); // Add one to the score variable 'count' count = count + 1; // Run the 'SetCountText()' function (see below) SetCountText(); } } // Create a standalone function that can update the 'countText' UI and check if the required amount to win has been achieved void SetCountText() { // Update the text field of our 'countText' variable countText.text = "Count: " + count.ToString(); // Check if our 'count' is equal to or exceeded 12 if (count >= 12) { // Set the text value of our 'winText' winText.text = "You Win!"; } } void SetMovesText() { movesText.text = "Moves: " + moves.ToString(); } } 通常是lodash或下划线,但你应该知道这些函数/库没有任何神秘或神奇的东西。你也可以制作一个。

_

答案 2 :(得分:2)

如果你不想要lodash / underscore(确切地说是_),你可以尝试例如ES7语法

Object.assign({}, state, {current_team: action.team})

{ ...state, current_team: action.team }