1. Rule 1: Object.Method()
1.1 Case 1function fn() { console.log(this.a + this.b); } var obj = { a: 66, b: 33, fn: fn } obj.fn(); Output:
1.2 Case 2var obj1 = { a: 66, b: 33, fn: function () { console.log(this.a + this.b); } } var obj2 = { a: 66, b: 33, fn: obj1.fn } obj2.fn(); Output:
1.3 Case 3function outer() { var a = 11; var b = 22; return { a: 33, b: 44, fn: function () { console.log(this.a + this.b); } } } outer.fn(); Output:
1.4 Case 4function fun() { console.log(this.a + this.b); } var obj = { a: 1, b: 2, c: [{ a: 3, b: 4, c: fun }] }; var a = 5; obj.c[0].c(); Output:
2. Rule 2: Function()
2.1 Case 1var obj1 = { a: 1, b: 2, fn: function () { console.log(this.a + this.b); } } var a = 3; var b = 4; var fn = obj1.fn; fn(); Output:
2.2 Case 2function fun() { return this.a + this.b; } var a = 1; var b = 2; var obj = { a: 3, b: fun(), // Apply rule 2 fun: fun } var result = obj.fun(); // Rule 1 applies console.log(result); Output:
3. Rule 3: Array subscript
3.1 Case 1var arr = ['A', 'B', 'C', function () { console.log(this[0]); }]; arr[3](); Output:
3.2 Case 2function fun() { arguments[3](); } fun('A', 'B', 'C', function () { console.log(this[1]); }); Output:
4. Rule 4: IIFE 4.1 Case 1var a = 1; var obj = { a: 2, fun: (function () { var a = this.a; // Rule 1 applies return function () { // Rule 4 applies console.log(a + this.a); // 2+1 } })() }; obj.fun(); Output:
5. Rule 5: Timers and Delays 5.1 Case 1var obj = { a: 1, b: 2, fun: function () { console.log(this.a + this.b); } } var a = 3; var b = 4; setTimeout(obj.fun, 2000); // Rule 5 applies Output:
5.2 Case 2var obj = { a: 1, b: 2, fun: function () { console.log(this.a + this.b); } } var a = 3; var b = 4; setTimeout(function () { obj.fun(); //Apply rule 1 }, 2000); Output:
6. Rule 6: Event Handling Function 6.1 Case 1Please achieve the effect: whichever box is clicked will turn red, and the same event handling function must be used to achieve this. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Rule 6: Event Handling Function</title> <style> * { margin: 0; padding: 0; } body div { float: left; width: 100px; height: 100px; margin-right: 10px; border: 1px solid #000; } </style> </head> <body> <div id="box1">1</div> <div id="box2">2</div> <div id="box3">3</div> <script> function changeColor() { this.style.backgroundColor = 'red'; } var box1 = document.getElementById('box1'); var box2 = document.getElementById('box2'); var box3 = document.getElementById('box3'); box1.onclick = changeColor; box2.onclick = changeColor; box3.onclick = changeColor; </script> </body> </html> Result: 6.2 Case 2Please achieve the effect: click on a box and the box will turn red after 2000 milliseconds. The same event handling function is required to achieve this. Difference from case 1: A backup context is required. function changeColor() { var self = this; // backup context setTimeout(function () { self.style.backgroundColor = 'red'; }, 2000); } var box1 = document.getElementById('box1'); var box2 = document.getElementById('box2'); var box3 = document.getElementById('box3'); box1.onclick = changeColor; box2.onclick = changeColor; box3.onclick = changeColor; This is the end of this article about the context rules in JavaScript functions. For more information about JavaScript function context rules, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of how to use Tomcat Native to improve Tomcat IO efficiency
>>: HTML Tutorial: HTML horizontal line segment
For historical reasons, MySQL replication is base...
Table of contents 1. First look at COUNT 2. The d...
The CentOS Project, a 100% compatible rebuild of ...
Solution: Add the following code in <head>: ...
Table of contents 1. System monitoring 2. File Op...
Nginx is a powerful, high-performance web and rev...
In order to improve user experience and ease of us...
Migration is unavoidable in many cases. Hardware ...
1. forEach() is similar to map(). It also applies...
Download from official website: https://www.mysql...
I have written such an article before, but I used...
1. Install the Linux system on the virtual machin...
1. Inline styles To add inline styles to the virt...
CSS attribute selectors are amazing. They can hel...
1. Download the alpine image [root@DockerBrian ~]...