Some things about objects and functions in JavaScript [the essence of JavaScript-N1]

Source: Internet
Author: User

When I was reading the essence of JavaScript language today, I always felt a bit difficult to understand about a part of the function. The Code is as follows: 1: var obj = (function () {2: var value = 0; 3: 4: return {5: increment: function (inc) {6: value + = typeof inc = "number "? Inc: 1; 7: }, 8: getValue: function () {9: return value; 10:} 11 :}; 12 :}()); pay attention to the yellow pair of parentheses at the end of the Code, which is described as follows: "We didn't assign a function to obj. We assigned it the result returned after calling this function, this function returns an object that contains two aspects, and these methods continue to enjoy the privilege to access the value variable ". In fact, code like this is used a lot, but it has never been carefully considered. If the last pair of parentheses is removed, a function (object) is returned. If we call the above obj in the code like this, we can use it directly. If we execute typeof obj, the returned result is "object", for example, 1: obj. increment (1); 2: alert (obj. getValue (); // 1 3: obj. increment (2); 4: alert (obj. getValue (); // 3 if the last pair of parentheses is removed and the returned result of typeof obj is "function", you must declare the initialization method before using this object, for example: 1: var o = obj (); 2: o. increment (1); 3: alert (o. getValue (); // 1 4: o. increment (2); 5: alert (o. getValue (); // 3 seems to understand the difference between parentheses. However, the following example of Fibonacci makes me suddenly realize that it still has such a good use. That is, the function can record the previous operation results in an object to avoid unnecessary repeated operations. We use a normal Recursion Method to Calculate the Fibonacci value. The Code is as follows: 1: var totalTimes = 0; 2: 3: var fibonacci = function (n) {4: totalTimes + = 1; 5: return n <2? N: Maid (n-1) + maid (n-2); 6:} 7: 8: function calculateFibonacci () {9: totalTimes = 0; 10: for (var I = 0; I <11; I ++) {11: document. getElementById ("rlt "). innerHTML + = "//" + I + ":" + maid (I) + "<br/>"; 12:} 13: document. getElementById ("rlt "). innerHTML + = "// totalTimes:" + totalTimes + "<br/>"; 14:} run twice in a row. The following result is displayed: 1: // 2: // 3: // 4: // 5: // 6: // 7: // 8: // 9: // 8: 21 10: // 11: // 12: // totalTimes: 453 13: // 14: // 15: // 16: // 17: // 18: // 19: // 20: // 21: // 22: // 23: // totalTimes: 453 yes. Both are executed 453 times. Because the calculation starts from 0, 1 every time. However, if we use the following code (note the last pair of parentheses): 1: var totalTimes = 0; 2: var fibonacciMemo = function () {3: var meno = [0, 1]; 4: var fib = function (n) {5: totalTimes + = 1; 6: var result = meno [n]; 7: if (typeof result! = "Number") {8: result = fib (n-1) + fib (n-2); 9: meno [n] = result; 10:} 11: return result; 12: 13:}; 14: return fib; 15:} (); 16: 17: function calculateFibonacciEx () 18: {19: totalTimes = 0; 20: for (var I = 0; I <11; I ++) {21: document. getElementById ("rlt "). innerHTML + = "//" + I + ":" + maid (I) + "<br/>"; 22:} 23: document. getElementById ("rlt "). innerHTML + = "// totalTimes:" + totalTimes + "<br/>"; 24:} continuous The result is as follows: 1: // 0: 0 2: // 3: // 4: // 5: // 6: // 7: // 8: // 9: // 10: // 11: // 12: // totalTimes: 29 13: // 14: // 15: // 16: // 17: // 18: // 19: // 20: // 21: // 22: // 23: // 24: // totalTimes: 11 yes. The first computation is 29, and the second computation is 11. The reason is that we saved the result in the meno array. Of course, we can save the results in other ways, such as in the form of global variables. However, global variables are one of the application and storage modes that the author strongly opposes, in addition, the storage of this computing result is transparent to the outside world. The caller only needs the computing result and does not need to know how to save it, or we do not need any (global) variable other than this computing function, or any person who later maintains this code, and do not need to be notified elsewhere, there are intermediate computing results that require him to do extra maintenance work! Of course, we don't have to worry about program efficiency or how slow your browser is! In fact, it has not been the same until now. The difference in usage between them or the advantages and disadvantages are there, because I have never figured out where the Point of the incident is, so the title of the blog post is always unclear! Hope you can give us some advice!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.