introducing this chapter, we will explain the strategy of passing parameters to function functions in ECMAScript. In computer science, this strategy is generally called "Evaluation strategy" (Uncle Note: Some people say translation into the evaluation strategy, some people translated into the assignment strategy, through the following content, I think called the assignment strategy more appropriate, anyway, The title is also written as an easy-to-understand evaluation strategy, for example, in a programming language to evaluate or evaluate an expression to set rules. The policy of passing parameters to a function is a special case. http://dmitrysoshnikov.com/ecmascript/chapter-8-evaluation-strategy/the reason for writing this article is because someone on the forum asked for an accurate explanation of some of the strategy of the communication, we give the corresponding definition here, we hope to be helpful to everyone. Many programmers are convinced that in JavaScript (and even some other languages), objects are passed by reference, while primitive value types are referenced by value, and many articles talk about this "fact", but how many people really understand the term and how much is right?Let's talk about this in this article. General theory needs to be noted that in the assignment theory there are generally 2 assignment strategies: strict-meaning that the parameters are calculated before entering the program, not strictly-meaning that the calculation of the parameters is calculated according to the calculation requirements (that is, the equivalent of a delay calculation). Then, here we consider the basic function of the strategy, from ECMAScript starting point is very important. The first thing to note is that strict parameter-passing strategies are used in ECMAScript (and even in other words, such as C,java,python and Ruby). It is also important to calculate the order in which the arguments are passed--in the ECMAScript is left to right, and the order of introspection in other languages (from right to do) is also available. Strict communication strategy is also divided into several seed strategies, the most important of which we discuss in detail in this chapter. The strategies discussed below are not all used in ECMAScript, so we use pseudo-code to demonstrate when discussing the specific behavior of these strategies. Passing by value is passed by value, and many developers understand that the value of the parameter is a copy of the value of the object passed by the caller (copy of value), and that changing the value of the parameter inside the function does not affect the outer object (the value outside the parameter), in general, the new memory is reassigned ( We are not concerned about how the allocation of memory is implemented-and the stack may be a dynamic memory allocation-the value of the new memory block is a copy of the external object, and its value is used inside the function. Bar= 10procedure foo (bararg): Bararg= 20; end foo (bar)//Foo Internal change value does not affect the value of the internal barPrint (BAR)//TenHowever, if the parameter of the function is not the original value but the complex structure object is the time, it will bring great performance problems, C++the problem is that when a struct is passed into a function as a value-it is a complete copy. Let us give a general example, using the following assignment strategy to test, think about a function to accept 2 parameters, the 1th parameter is the value of the object, the 2nd is a Boolean tag, used to mark whether to completely modify the incoming object (to reassign the object), or just modify some properties of the object. //Note: The following are pseudo-code, not JS implementationBar ={x:10, y:20} procedure foo (Bararg, isfullchange):ifIsfullchange:bararg= {Z:1, q:2} exit End bararg.x= 100bararg.y= 200end foo (bar)//passed by value, external objects are not changedPrint (BAR)//{x:10, y:20} //changing objects completely (assigning new values)Foo (bar,true) //it hasn't changed .Print (BAR)//{x:10, y:20}, not {z:1, q:2}passing by reference another well-known pass-by-reference is not a value copy, but an implicit reference to an object, such as an external direct reference address of the object. Any change to a parameter within a function is a value that affects the outside of the function, because both refer to the same object, that is, the argument is equivalent to an alias of an external object. Pseudo Code: Procedure foo (Bararg, isfullchange):ifIsfullchange:bararg= {Z:1, q:2} exit End bararg.x= 100bararg.y= 200End//use the same object as the example aboveBar ={x:10, y:20} //the result of invoking by reference is as follows:foo (bar)//the property value of the object has been changed.Print (BAR)//{x:100, y:200} //re-assigning a new value also affects the objectFoo (bar,true) //now the object is already a new object.Print (BAR)//{z:1, q:2}This strategy can pass complex objects more efficiently, such as large-structure objects with large batch properties. By share delivery (call by sharing) above 2 strategies everyone knows, but a strategy here may not be well understood (in fact, an academic strategy). However, we will soon see that this is exactly the strategy that plays a key role in the parameter-passing strategy in ECMAScript. This strategy also has some pronouns: "pass by object" or "pass by object sharing". This strategy was introduced in 1974 by Barbara Liskov for the CLU programming language. The point of this strategy is that the function receives a copy (copy) of the object for which the reference copy is associated with the formal parameter and its value. The reference that appears here, we cannot call it "passing by reference" because the function receives a parameter that is not a direct object alias, but rather a copy of the reference address. The most important difference is that the re-assignment of a new value inside the function does not affect the external object (and the case passed by reference in the example above), but because the parameter is an address copy, it is accessed outside the same object (for example, the external object does not want to pass the same exact copy as the value). Changing the property value of the Parameter object will affect the external object. Procedure foo (Bararg, isfullchange):ifIsfullchange:bararg= {Z:1, q:2} exit End bararg.x= 100bararg.y= 200End//or do you use this object structure?Bar ={x:10, y:20} //transfer by contribution affects the objectfoo (bar)//the properties of the object have been modifiedPrint (BAR)//{x:100, y:200} //re-assignment does not workFoo (bar,true) //is still the value abovePrint (BAR)//{x:100, y:200}The premise of this processing is the object used in most languages, not the original value. Pass-by-share delivery is a special case by value passing this policy is used in many languages: Java, ECMAScript, Python, Ruby, Visual Basic, etc. In addition, the Python community has used the term, and other languages can use the term because other names tend to make people feel confused. In most cases, such as in Java,ecmascript or Visual Basic, this strategy is also known as passing by value-meaning: Special values-reference copies (replicas). On the one hand, it is this--the argument passed to the function is simply a name of the bound value (the reference address) and does not affect the outer object. On the other hand, without deep research, these terms are really considered to be eating wrong, because many forums are talking about how to pass objects to JavaScript functions. The general theory does have a claim by value: But at this point the value is what we call the copy of the address, so we don't break the rules. In Ruby, this policy is referred to as passing by reference. Again: It is not passed as a copy of a large structure (for example, not by value), but on the other hand, we do not have a reference to the original object and cannot modify it, so the concept of cross-terminology can be more confusing. There is no special case for interviewing by reference, as in the case of a special case passed by value. But it is still necessary to understand these strategies in the above mentioned techniques (Java, ECMAScript, Python, Ruby, other), actually-the strategy they use is to pass by share. Press share with pointer for с/с+ +, this strategy is the same in thought and by pointer value, but there is an important difference-the strategy can dereference pointers and change objects completely. In general, however, assigning a value (address) pointer to a new block of memory (that is, the memory block previously referenced remains the same), and changing the object's properties by a pointer affects the Antoine external object.so, and pointer categories, we can obviously see that this is passed by address value. In this case, passing by share is just "syntactic sugar," like pointer assignment behavior (but not dereferencing), or modifying a property like a reference (no dereference is required), and sometimes it can be named "Safe pointer." However, С/с+ + If you reference an object property without a clear pointer dereference, you also have a special syntax sugar:obj->x instead of (*obj). x and C+ + The most closely related ideology can be seen in the implementation of the "smart pointer", for example, in the boost:: shared_ptr, overloading the assignment operator and copy constructors, and also using the object's reference counter to delete the object through the GC. This type of data, even with a similar name--share _ptr. ECMAScript implementation now we know the policy of passing an object as a parameter in ECMAScript-by share delivery: Modifying the properties of a parameter will affect the external, and the re-assignment will not affect the external object. However, as we mentioned above, the ECMAScript developers generally call it: pass by value, except that the value is a copy of the reference address. The JavaScript inventor, Brendan Aichy, also writes that a reference copy (address copy) is passed. So the forum has been said by the value of the transmission, in this interpretation, is also right. Rather, this behavior can be understood as a simple assignment, and we can see that the interior is a completely different object, except that it refers to the same value-that is, the copy of the address. ECMAScript code:varFoo = {x:10, y:20};varBar =foo; alert (Bar= = = foo);//truebar.x= 100; Bar.y= 200; alert ([Foo.x, Foo.y]);//[A]that is, two identifiers (name bindings) are bound to the same object in memory, sharing this object: Foo value:addr (0xFF) = {x:100, y:200} (address 0xFF) <= Bar value:addr (0xFFThe binding is the new object identifier (the new address) and does not affect objects that have been previously bound: bar= {Z:1, q:2}; alert ([Foo.x, Foo.y]); //[100, 200]– not changedalert ([Bar.z, bar.q]);//[1, 2]– but now refers to a new objectnow Foo and bar, there are different values and different addresses: Foo value:addr (0xFF) = {x:100, y:200} (address 0xFF) Bar value:addr (0xFA) = {z:1, q:2} (address 0xFAagain, the value of the object described here is the address, not the object structure itself, assigning the variable to another variable--a reference to the assignment value. So two variables refer to the same memory address. The next assignment is the new address, which resolves the address binding to the old object, and then binds to the address of the new object, which is the most important difference that is passed by reference. Also, if you only consider the ECMA-The level of abstraction provided by the 262 standard, we see in the algorithm only the concept of "value", the implementation of the "value" (Can be the original value, also can be an object), but according to our definition above, can also be called "by value", because the reference address is also a value. However, in order to avoid misunderstandings (why the properties of external objects can be changed inside a function), there is still a need to consider the implementation level of detail--we see passing by share, or in other words--by a security pointer, and the security pointer is not able to dereference and change the object, but can go to modify the object's property values. The term version lets us define the term version of the policy in ECMAScript. It can be called "passing by Value"-The value here is a special case, that is, the value is a copy of the address. From this level we can say that objects other than exceptions in ECMAScript are passed by value, which is actually the ECMAScript abstraction level. Or in this case, specifically referred to as "by share delivery," through this can see the traditional by-value and by reference to pass the difference, this situation can be divided into 2 kinds of situations:1: The original value is passed by value; 2: Objects are passed by share. The phrase "object to function by reference type" is irrelevant to ECMAScript, and it is wrong. Conclusion I hope this article will help you learn more about the details and the implementation in ECMAScript. As always, if there are any questions, welcome to the discussion. Other references evaluation Strategycall by Valuecall by Referencecall Sharing synchronization and recommendation this article has been synchronized to the directory index: in-depth understanding of JavaScript series in-depth understanding of the JavaScript series, including the original, translation, reprint and other types of articles, if it is useful to you, please recommend supporting a, to the power of the uncle writing.
In-depth understanding of the JavaScript series (19): Evaluation strategy (Evaluation strategy)