B2BÍøÂçÈí¼þ

 ÕÒ»ØÃÜÂë
 Á¢¼´×¢²á ÉóºËÍøÕ¾ºÅ:QQ:896757558
ËÑË÷
²é¿´: 4|»Ø¸´: 0
´òÓ¡ ÉÏÒ»Ö÷Ìâ ÏÂÒ»Ö÷Ìâ

JavaScriptÓ﷨СʾÀý

[¸´ÖÆÁ´½Ó]

1093

Ö÷Ìâ

1098

Ìû×Ó

7649

»ý·Ö

abc

Rank: 9Rank: 9Rank: 9

»ý·Ö
7649
Ìø×ªµ½Ö¸¶¨Â¥²ã
Â¥Ö÷
JavaScriptÓ﷨СʾÀý
JavaScriptÓ﷨СʾÀý B2BÍøÂçÈí¼þ

JavaScriptÓ﷨СʾÀý B2BÍøÂçÈí¼þ

  1. // JavaScript ¸ß¼¶¹¦ÄÜ×ÛºÏʾÀý
  2. // »ùÓÚ AIWROK Èí¼þ¼¼ÊõÎĵµÖÐµÄ JavaScript Óï¾ä˵Ã÷
  3. // °üº¬»ù´¡¸ÅÄîºÍ¸ß¼¶¹¦ÄܵÄÍêÕûÑÝʾ

  4. // ==================== µÚÒ»²¿·Ö£º»ù´¡¸ÅÄî ====================

  5. console.log("\n========== JavaScript »ù´¡¸ÅÄîÑÝʾ¿ªÊ¼ ==========");
  6. sleep.second(Ãë=2);

  7. // 1. JavaScript Óï¾ä - Ó÷ֺŷָôµÄÃüÁî
  8. console.log("\n¡¾1¡¿JavaScript Óï¾äʾÀý");
  9. var x = 5 + 6;  // Êý×ÖÔËËã
  10. var y = x * 10; // ±í´ïʽ¼ÆËã
  11. console.log("x = " + x + ", y = " + y);
  12. sleep.second(Ãë=2);

  13. // 2. JavaScript ¹Ø¼ü×ÖʹÓÃʾÀý
  14. console.log("\n¡¾2¡¿JavaScript ¹Ø¼ü×ÖʹÓÃʾÀý");
  15. var firstName = "John";    // var ¹Ø¼ü×ÖÉùÃ÷±äÁ¿
  16. var lastName = "Doe";      // ×Ö·û´®¸³Öµ
  17. var age = 30;              // Êý×Ö¸³Öµ
  18. var isActive = true;       // ²¼¶ûÖµ
  19. console.log("ÐÕÃû: " + firstName + " " + lastName + ", ÄêÁä: " + age + ", ¼¤»î״̬: " + isActive);
  20. sleep.second(Ãë=2);

  21. // 3. JavaScript ×¢ÊÍʾÀý
  22. // ÕâÊǵ¥ÐÐ×¢ÊÍ£¬²»»á±»Ö´ÐÐ
  23. /*
  24.    ÕâÊǶàÐÐ×¢ÊÍ
  25.    Ò²²»»á±»Ö´ÐÐ
  26. */

  27. // 4. JavaScript Êý¾ÝÀàÐÍʾÀý
  28. console.log("\n¡¾4¡¿JavaScript Êý¾ÝÀàÐÍʾÀý");
  29. var length = 16;                                    // Number ÀàÐÍ
  30. var points = x * 10;                                // Number ͨ¹ý±í´ïʽ
  31. var fullName = firstName + " " + lastName;          // String ×Ö·û´®Æ´½Ó
  32. var hobbies = ["ÔĶÁ", "±à³Ì", "Ô˶¯"];             // Array Êý×é
  33. var person = {                                      // Object ¶ÔÏó
  34.     firstName: firstName,
  35.     lastName: lastName,
  36.     age: age,
  37.     isActive: isActive
  38. };
  39. console.log("³¤¶È: " + length + ", ·ÖÊý: " + points);
  40. console.log("È«Ãû: " + fullName);
  41. console.log("°®ºÃÊýÁ¿: " + hobbies.length);
  42. sleep.second(Ãë=2);

  43. // 5. Êý¾ÝÀàÐͲÙ×÷ʾÀý
  44. console.log("\n¡¾5¡¿Êý¾ÝÀàÐͲÙ×÷ʾÀý");
  45. var numStr = 16 + "Volvo";  // Êý×ÖÓë×Ö·û´®Æ´½Ó½á¹ûΪ "16Volvo"
  46. console.log("Êý×ÖÓë×Ö·û´®Æ´½Ó½á¹û: " + numStr);
  47. sleep.second(Ãë=2);

  48. // 6. JavaScript º¯ÊýʾÀý
  49. console.log("\n¡¾6¡¿JavaScript º¯ÊýʾÀý");
  50. function calculateArea(width, height) {
  51.     return width * height;  // ·µ»ØÃæ»ý¼ÆËã½á¹û
  52. }

  53. function greetUser(name) {
  54.     return "ÄãºÃ, " + name + "!";  // ·µ»ØÎʺòÓï
  55. }

  56. // µ÷Óú¯Êý
  57. var area = calculateArea(5, 10);
  58. var greeting = greetUser(firstName);

  59. console.log("¾ØÐÎÃæ»ý (5x10): " + area);
  60. console.log(greeting);
  61. sleep.second(Ãë=2);

  62. // 7. ´óСдÃô¸ÐÐÔʾÀý
  63. console.log("\n¡¾7¡¿´óСдÃô¸ÐÐÔʾÀý");
  64. var myVariable = "Сд±äÁ¿";
  65. var MyVariable = "´óд¿ªÍ·±äÁ¿";
  66. // ×¢Ò⣺myVariable ºÍ MyVariable ÊÇÁ½¸ö²»Í¬µÄ±äÁ¿

  67. console.log("myVariable: " + myVariable);
  68. console.log("MyVariable: " + MyVariable);
  69. console.log("Á½¸ö±äÁ¿²»Í¬: " + (myVariable !== MyVariable));
  70. sleep.second(Ãë=2);

  71. // 8. ¸ü¶à¹Ø¼ü×ÖʹÓÃʾÀý
  72. console.log("\n¡¾8¡¿¿ØÖÆÁ÷¹Ø¼ü×ÖʾÀý");
  73. if (age >= 18) {
  74.     console.log("ÄêÁä " + age + " - ³ÉÄêÈË");
  75. } else {
  76.     console.log("ÄêÁä " + age + " - δ³ÉÄêÈË");
  77. }

  78. console.log("\n±éÀú°®ºÃÁбí:");
  79. for (var i = 0; i < hobbies.length; i++) {
  80.     console.log("  °®ºÃ " + (i+1) + ": " + hobbies[i]);
  81. }
  82. sleep.second(Ãë=2);

  83. // 9. switch Óï¾äʾÀý
  84. console.log("\n¡¾9¡¿switch Óï¾äʾÀý");
  85. var day = 3;
  86. var dayName;
  87. switch (day) {
  88.     case 1:
  89.         dayName = "ÐÇÆÚÒ»";
  90.         break;
  91.     case 2:
  92.         dayName = "ÐÇÆÚ¶þ";
  93.         break;
  94.     case 3:
  95.         dayName = "ÐÇÆÚÈý";
  96.         break;
  97.     default:
  98.         dayName = "ÆäËûÈÕÆÚ";
  99. }
  100. console.log("½ñÌìÊÇ: " + dayName);
  101. sleep.second(Ãë=2);

  102. // 10. try-catch ´íÎó´¦ÀíʾÀý
  103. console.log("\n¡¾10¡¿try-catch ´íÎó´¦ÀíʾÀý");
  104. try {
  105.     var result = undefinedVariable + 10;  // Õâ»áÒý·¢´íÎó
  106. } catch (error) {
  107.     console.log("²¶»ñµ½´íÎó: " + error.message);
  108.     console.log("´íÎó´¦Àí³É¹¦£¡");
  109. }
  110. sleep.second(Ãë=2);

  111. // 11. Êý×é²Ù×÷·½·¨
  112. console.log("\n¡¾11¡¿Êý×é²Ù×÷·½·¨Ê¾Àý");
  113. var numbers = [1, 2, 3, 4, 5];
  114. console.log("ԭʼÊý×é: " + numbers.join(", "));
  115. numbers.push(6);  // Ìí¼ÓÔªËØ
  116. console.log("pushºó: " + numbers.join(", "));
  117. numbers.pop();    // ÒÆ³ý×îºóÒ»¸öÔªËØ
  118. console.log("popºó: " + numbers.join(", "));
  119. sleep.second(Ãë=2);

  120. // 12. ¶ÔÏó·½·¨Ê¾Àý
  121. console.log("\n¡¾12¡¿¶ÔÏó·½·¨Ê¾Àý");
  122. person.getFullName = function() {
  123.     return this.firstName + " " + this.lastName;
  124. };

  125. console.log("ÍêÕûÐÕÃû: " + person.getFullName());
  126. console.log("ÈËÔ±ÐÅÏ¢: " + JSON.stringify(person));
  127. sleep.second(Ãë=2);

  128. // ==================== µÚ¶þ²¿·Ö£º¸ß¼¶¹¦ÄÜ ====================

  129. console.log("\n========== JavaScript ¸ß¼¶¹¦ÄÜÑÝʾ¿ªÊ¼ ==========");
  130. sleep.second(Ãë=2);

  131. // 13. ±Õ°üʾÀý
  132. console.log("\n¡¾13¡¿±Õ°üʾÀý");
  133. function createCounter() {
  134.     var count = 0;
  135.     return {
  136.         increment: function() {
  137.             count++;
  138.             return count;
  139.         },
  140.         decrement: function() {
  141.             count--;
  142.             return count;
  143.         },
  144.         getCount: function() {
  145.             return count;
  146.         }
  147.     };
  148. }

  149. var counter = createCounter();
  150. console.log("¼ÆÊýÆ÷³õʼֵ: " + counter.getCount());
  151. console.log("µÝÔöºó: " + counter.increment());
  152. console.log("ÔÙµÝÔö: " + counter.increment());
  153. console.log("µÝ¼õºó: " + counter.decrement());
  154. sleep.second(Ãë=2);

  155. // 14. µÝ¹éº¯ÊýʾÀý
  156. console.log("\n¡¾14¡¿µÝ¹éº¯ÊýʾÀý");
  157. function factorial(n) {
  158.     if (n <= 1) return 1;
  159.     return n * factorial(n - 1);
  160. }

  161. console.log("5µÄ½×³Ë: " + factorial(5));
  162. console.log("10µÄ½×³Ë: " + factorial(10));
  163. sleep.second(Ãë=2);

  164. // 15. ¸ß½×º¯ÊýʾÀý
  165. console.log("\n¡¾15¡¿¸ß½×º¯ÊýʾÀý");
  166. function applyOperation(arr, operation) {
  167.     var result = [];
  168.     for (var i = 0; i < arr.length; i++) {
  169.         result.push(operation(arr[i]));
  170.     }
  171.     return result;
  172. }

  173. var squaredNumbers = applyOperation([1, 2, 3, 4, 5], function(num) {
  174.     return num * num;
  175. });
  176. console.log("ԭʼÊý×é: [1, 2, 3, 4, 5]");
  177. console.log("ƽ·½Êý×é: " + squaredNumbers.join(", "));
  178. sleep.second(Ãë=2);

  179. // 16. Ô­ÐÍÁ´Ê¾Àý
  180. console.log("\n¡¾16¡¿Ô­ÐÍÁ´¼Ì³ÐʾÀý");
  181. function Animal(name, type) {
  182.     this.name = name;
  183.     this.type = type;
  184. }

  185. Animal.prototype.speak = function() {
  186.     return this.name + " ·¢³öÁËÉùÒô";
  187. };

  188. function Dog(name) {
  189.     Animal.call(this, name, "¹·");
  190. }

  191. Dog.prototype = Object.create(Animal.prototype);
  192. Dog.prototype.constructor = Dog;
  193. Dog.prototype.bark = function() {
  194.     return this.name + " ÍôÍô½Ð";
  195. };

  196. var myDog = new Dog("Íú²Æ");
  197. console.log(myDog.speak());
  198. console.log(myDog.bark());
  199. sleep.second(Ãë=2);

  200. // 17. Promise Ä£Ä⣨ÓÉÓÚ»·¾³ÏÞÖÆ£¬Ê¹Óûص÷·½Ê½£©
  201. console.log("\n¡¾17¡¿Òì²½±à³ÌʾÀý");
  202. function asyncOperation(data, callback) {
  203.     setTimeout(function() {
  204.         var result = "´¦ÀíÍê³É: " + data;
  205.         callback(null, result);
  206.     }, 100);
  207. }

  208. asyncOperation("²âÊÔÊý¾Ý", function(error, result) {
  209.     if (error) {
  210.         console.log("´íÎó: " + error);
  211.     } else {
  212.         console.log(result);
  213.     }
  214. });
  215. sleep.second(Ãë=2);

  216. // 18. Ä£¿é»¯Ä£Ê½
  217. console.log("\n¡¾18¡¿Ä£¿é»¯Ä£Ê½Ê¾Àý");
  218. var MathUtils = (function() {
  219.     // ˽ÓбäÁ¿ºÍ·½·¨
  220.     var PI = 3.14159265359;
  221.    
  222.     function validateNumber(num) {
  223.         return typeof num === 'number' && !isNaN(num);
  224.     }
  225.    
  226.     // ¹«¹²API
  227.     return {
  228.         circleArea: function(radius) {
  229.             if (!validateNumber(radius) || radius < 0) {
  230.                 throw new Error("ÎÞЧµÄ°ë¾¶Öµ");
  231.             }
  232.             return PI * radius * radius;
  233.         },
  234.         rectangleArea: function(width, height) {
  235.             if (!validateNumber(width) || !validateNumber(height)) {
  236.                 throw new Error("ÎÞЧµÄ³ß´çÖµ");
  237.             }
  238.             return width * height;
  239.         },
  240.         add: function(a, b) {
  241.             if (!validateNumber(a) || !validateNumber(b)) {
  242.                 throw new Error("ÎÞЧµÄÊý×Ö²ÎÊý");
  243.             }
  244.             return a + b;
  245.         }
  246.     };
  247. })();

  248. console.log("Ô²Ãæ»ý (r=5): " + MathUtils.circleArea(5));
  249. console.log("¾ØÐÎÃæ»ý (5x10): " + MathUtils.rectangleArea(5, 10));
  250. console.log("¼Ó·¨ (3+7): " + MathUtils.add(3, 7));
  251. sleep.second(Ãë=2);

  252. // 19. ʼþÄ£ÄâÆ÷
  253. console.log("\n¡¾19¡¿Ê¼þϵͳʾÀý");
  254. var EventEmitter = function() {
  255.     this.events = {};
  256. };

  257. EventEmitter.prototype.on = function(event, listener) {
  258.     if (!this.events[event]) {
  259.         this.events[event] = [];
  260.     }
  261.     this.events[event].push(listener);
  262. };

  263. EventEmitter.prototype.emit = function(event) {
  264.     if (this.events[event]) {
  265.         var args = Array.prototype.slice.call(arguments, 1);
  266.         this.events[event].forEach(function(listener) {
  267.             listener.apply(null, args);
  268.         });
  269.     }
  270. };

  271. var emitter = new EventEmitter();
  272. emitter.on('greet', function(name) {
  273.     console.log("»¶Ó­, " + name + "!");
  274. });

  275. emitter.on('data', function(data) {
  276.     console.log("ÊÕµ½Êý¾Ý: " + data);
  277. });

  278. emitter.emit('greet', 'Óû§');
  279. emitter.emit('data', 'ÖØÒªÐÅÏ¢');
  280. sleep.second(Ãë=2);

  281. // 20. Êý¾Ý½á¹¹ÊµÏÖ - Õ»
  282. console.log("\n¡¾20¡¿Õ» (Stack) Êý¾Ý½á¹¹Ê¾Àý");
  283. function Stack() {
  284.     this.items = [];
  285. }

  286. Stack.prototype.push = function(item) {
  287.     this.items.push(item);
  288. };

  289. Stack.prototype.pop = function() {
  290.     if (this.isEmpty()) {
  291.         return null;
  292.     }
  293.     return this.items.pop();
  294. };

  295. Stack.prototype.peek = function() {
  296.     if (this.isEmpty()) {
  297.         return null;
  298.     }
  299.     return this.items[this.items.length - 1];
  300. };

  301. Stack.prototype.isEmpty = function() {
  302.     return this.items.length === 0;
  303. };

  304. Stack.prototype.size = function() {
  305.     return this.items.length;
  306. };

  307. var stack = new Stack();
  308. stack.push(1);
  309. stack.push(2);
  310. stack.push(3);
  311. console.log("Õ»¶¥ÔªËØ: " + stack.peek());
  312. console.log("µ¯³öÔªËØ: " + stack.pop());
  313. console.log("Õ»´óС: " + stack.size());
  314. sleep.second(Ãë=2);

  315. // 21. Êý¾Ý½á¹¹ÊµÏÖ - ¶ÓÁÐ
  316. console.log("\n¡¾21¡¿¶ÓÁÐ (Queue) Êý¾Ý½á¹¹Ê¾Àý");
  317. function Queue() {
  318.     this.items = [];
  319. }

  320. Queue.prototype.enqueue = function(item) {
  321.     this.items.push(item);
  322. };

  323. Queue.prototype.dequeue = function() {
  324.     if (this.isEmpty()) {
  325.         return null;
  326.     }
  327.     return this.items.shift();
  328. };

  329. Queue.prototype.front = function() {
  330.     if (this.isEmpty()) {
  331.         return null;
  332.     }
  333.     return this.items[0];
  334. };

  335. Queue.prototype.isEmpty = function() {
  336.     return this.items.length === 0;
  337. };

  338. Queue.prototype.size = function() {
  339.     return this.items.length;
  340. };

  341. var queue = new Queue();
  342. queue.enqueue("ÈÎÎñ1");
  343. queue.enqueue("ÈÎÎñ2");
  344. queue.enqueue("ÈÎÎñ3");
  345. console.log("¶ÓÊ×ÈÎÎñ: " + queue.front());
  346. console.log("Íê³ÉÈÎÎñ: " + queue.dequeue());
  347. console.log("Ê£ÓàÈÎÎñÊý: " + queue.size());
  348. sleep.second(Ãë=2);

  349. // 22. ÕýÔò±í´ïʽʾÀý
  350. console.log("\n¡¾22¡¿ÕýÔò±í´ïʽÑé֤ʾÀý");
  351. function validateEmail(email) {
  352.     var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  353.     return emailRegex.test(email);
  354. }

  355. function validatePhone(phone) {
  356.     var phoneRegex = /^1[3-9]\d{9}$/;
  357.     return phoneRegex.test(phone);
  358. }

  359. console.log("ÓÊÏäÑéÖ¤ test@example.com: " + validateEmail("test@example.com"));
  360. console.log("ÊÖ»úºÅÑéÖ¤ 13812345678: " + validatePhone("13812345678"));
  361. sleep.second(Ãë=2);

  362. // 23. JSON ²Ù×÷ʾÀý
  363. console.log("\n¡¾23¡¿JSON Êý¾Ý´¦ÀíʾÀý");
  364. var userData = {
  365.     id: 1,
  366.     name: "ÕÅÈý",
  367.     age: 25,
  368.     hobbies: ["¶ÁÊé", "ÓÎÓ¾"],
  369.     address: {
  370.         city: "±±¾©",
  371.         street: "³¤°²½Ö100ºÅ"
  372.     }
  373. };

  374. var jsonString = JSON.stringify(userData);
  375. console.log("JSON×Ö·û´®: " + jsonString);

  376. var parsedData = JSON.parse(jsonString);
  377. console.log("½âÎöºóµÄÃû×Ö: " + parsedData.name);
  378. console.log("½âÎöºóµÄ³ÇÊÐ: " + parsedData.address.city);
  379. sleep.second(Ãë=2);

  380. // 24. ×Ö·û´®´¦Àí·½·¨
  381. console.log("\n¡¾24¡¿×Ö·û´®´¦Àí·½·¨Ê¾Àý");
  382. var text = "Hello World JavaScript";
  383. console.log("ת´óд: " + text.toUpperCase());
  384. console.log("תСд: " + text.toLowerCase());
  385. console.log("×Ó×Ö·û´®: " + text.substring(0, 5));
  386. console.log("Ìæ»»: " + text.replace("World", "ÓîÖæ"));
  387. console.log("·Ö¸î: " + text.split(" ").join("-"));
  388. sleep.second(Ãë=2);

  389. // 25. ÈÕÆÚºÍʱ¼ä´¦Àí
  390. console.log("\n¡¾25¡¿ÈÕÆÚºÍʱ¼ä´¦ÀíʾÀý");
  391. var now = new Date();
  392. console.log("µ±Ç°Ê±¼ä: " + now.toLocaleString());
  393. console.log("Äê·Ý: " + now.getFullYear());
  394. console.log("Ô·Ý: " + (now.getMonth() + 1));
  395. console.log("ÈÕÆÚ: " + now.getDate());
  396. console.log("Сʱ: " + now.getHours());
  397. console.log("·ÖÖÓ: " + now.getMinutes());
  398. console.log("ÃëÊý: " + now.getSeconds());
  399. sleep.second(Ãë=2);

  400. // 26. Map Êý¾Ý½á¹¹
  401. console.log("\n¡¾26¡¿Map Êý¾Ý½á¹¹Ê¾Àý");
  402. function CustomMap() {
  403.     this.keys = [];
  404.     this.values = [];
  405. }

  406. CustomMap.prototype.set = function(key, value) {
  407.     var index = this.keys.indexOf(key);
  408.     if (index !== -1) {
  409.         this.values[index] = value;
  410.     } else {
  411.         this.keys.push(key);
  412.         this.values.push(value);
  413.     }
  414. };

  415. CustomMap.prototype.get = function(key) {
  416.     var index = this.keys.indexOf(key);
  417.     if (index !== -1) {
  418.         return this.values[index];
  419.     }
  420.     return undefined;
  421. };

  422. CustomMap.prototype.has = function(key) {
  423.     return this.keys.indexOf(key) !== -1;
  424. };

  425. CustomMap.prototype.delete = function(key) {
  426.     var index = this.keys.indexOf(key);
  427.     if (index !== -1) {
  428.         this.keys.splice(index, 1);
  429.         this.values.splice(index, 1);
  430.         return true;
  431.     }
  432.     return false;
  433. };

  434. CustomMap.prototype.size = function() {
  435.     return this.keys.length;
  436. };

  437. var map = new CustomMap();
  438. map.set("name", "ÀîËÄ");
  439. map.set("age", 28);
  440. map.set("city", "ÉϺ£");
  441. console.log("MapÖлñÈ¡name: " + map.get("name"));
  442. console.log("MapÖÐÊÇ·ñ´æÔÚage: " + map.has("age"));
  443. console.log("Map´óС: " + map.size());
  444. map.delete("city");
  445. console.log("ɾ³ýcityºóMap´óС: " + map.size());
  446. sleep.second(Ãë=2);

  447. // 27. Set Êý¾Ý½á¹¹
  448. console.log("\n¡¾27¡¿Set Êý¾Ý½á¹¹Ê¾Àý");
  449. function CustomSet() {
  450.     this.items = {};
  451. }

  452. CustomSet.prototype.add = function(value) {
  453.     this.items[value] = value;
  454. };

  455. CustomSet.prototype.has = function(value) {
  456.     return this.items.hasOwnProperty(value);
  457. };

  458. CustomSet.prototype.delete = function(value) {
  459.     if (this.has(value)) {
  460.         delete this.items[value];
  461.         return true;
  462.     }
  463.     return false;
  464. };

  465. CustomSet.prototype.size = function() {
  466.     return Object.keys(this.items).length;
  467. };

  468. CustomSet.prototype.values = function() {
  469.     return Object.keys(this.items);
  470. };

  471. var set = new CustomSet();
  472. set.add(1);
  473. set.add(2);
  474. set.add(3);
  475. set.add(2); // ÖØ¸´Öµ²»»áÌí¼Ó
  476. console.log("SetÖеÄÖµ: " + set.values().join(", "));
  477. console.log("Set´óС: " + set.size());
  478. console.log("ÊÇ·ñ°üº¬2: " + set.has(2));
  479. set.delete(1);
  480. console.log("ɾ³ý1ºóSet´óС: " + set.size());
  481. sleep.second(Ãë=2);

  482. // 28. ¹Û²ìÕßģʽ
  483. console.log("\n¡¾28¡¿¹Û²ìÕßģʽʾÀý");
  484. function Subject() {
  485.     this.observers = [];
  486. }

  487. Subject.prototype.addObserver = function(observer) {
  488.     this.observers.push(observer);
  489. };

  490. Subject.prototype.removeObserver = function(observer) {
  491.     var index = this.observers.indexOf(observer);
  492.     if (index > -1) {
  493.         this.observers.splice(index, 1);
  494.     }
  495. };

  496. Subject.prototype.notify = function(data) {
  497.     this.observers.forEach(function(observer) {
  498.         observer.update(data);
  499.     });
  500. };

  501. function Observer(name) {
  502.     this.name = name;
  503. }

  504. Observer.prototype.update = function(data) {
  505.     console.log(this.name + " ÊÕµ½¸üÐÂ: " + data);
  506. };

  507. var subject = new Subject();
  508. var observer1 = new Observer("¹Û²ìÕß1");
  509. var observer2 = new Observer("¹Û²ìÕß2");

  510. subject.addObserver(observer1);
  511. subject.addObserver(observer2);
  512. subject.notify("״̬¸Ä±ä");
  513. sleep.second(Ãë=2);

  514. // 29. ¹¤³§Ä£Ê½
  515. console.log("\n¡¾29¡¿¹¤³§Ä£Ê½Ê¾Àý");
  516. function createUser(type, name, age) {
  517.     var user = {
  518.         name: name,
  519.         age: age,
  520.         type: type
  521.     };
  522.    
  523.     if (type === "admin") {
  524.         user.permissions = ["read", "write", "delete"];
  525.     } else if (type === "user") {
  526.         user.permissions = ["read"];
  527.     }
  528.    
  529.     user.getInfo = function() {
  530.         return this.name + " (" + this.type + "), ÄêÁä: " + this.age;
  531.     };
  532.    
  533.     return user;
  534. }

  535. var admin = createUser("admin", "¹ÜÀíÔ±", 35);
  536. var regularUser = createUser("user", "ÆÕͨÓû§", 25);

  537. console.log(admin.getInfo());
  538. console.log("¹ÜÀíԱȨÏÞ: " + admin.permissions.join(", "));
  539. console.log(regularUser.getInfo());
  540. console.log("ÆÕͨÓû§È¨ÏÞ: " + regularUser.permissions.join(", "));
  541. sleep.second(Ãë=2);

  542. // 30. ×°ÊÎÆ÷ģʽ
  543. console.log("\n¡¾30¡¿×°ÊÎÆ÷ģʽʾÀý");
  544. function Coffee(price) {
  545.     this.price = price;
  546. }

  547. Coffee.prototype.cost = function() {
  548.     return this.price;
  549. };

  550. function MilkDecorator(coffee) {
  551.     this.coffee = coffee;
  552. }

  553. MilkDecorator.prototype.cost = function() {
  554.     return this.coffee.cost() + 5;
  555. };

  556. function SugarDecorator(coffee) {
  557.     this.coffee = coffee;
  558. }

  559. SugarDecorator.prototype.cost = function() {
  560.     return this.coffee.cost() + 2;
  561. };

  562. var basicCoffee = new Coffee(10);
  563. var milkCoffee = new MilkDecorator(basicCoffee);
  564. var sweetMilkCoffee = new SugarDecorator(milkCoffee);

  565. console.log("»ù´¡¿§·È¼Û¸ñ: " + basicCoffee.cost());
  566. console.log("¼ÓÄÌ¿§·È¼Û¸ñ: " + milkCoffee.cost());
  567. console.log("¼ÓÌǼÓÄÌ¿§·È¼Û¸ñ: " + sweetMilkCoffee.cost());
  568. sleep.second(Ãë=2);

  569. // Êä³öËùÓÐʾÀý½á¹û×ܽá
  570. console.log("\n========== JavaScript ÑÝʾÍê³É ==========");
  571. console.log("\nÒÑÑÝʾÒÔϹ¦ÄÜ:");
  572. console.log("1. »ù´¡Óï·¨ºÍÊý¾ÝÀàÐÍ");
  573. console.log("2. º¯ÊýºÍ±Õ°ü");
  574. console.log("3. ÃæÏò¶ÔÏó±à³Ì");
  575. console.log("4. Òì²½±à³Ìģʽ");
  576. console.log("5. Ä£¿é»¯Éè¼Æ");
  577. console.log("6. ʼþϵͳ");
  578. console.log("7. Êý¾Ý½á¹¹ÊµÏÖ (Õ»¡¢¶ÓÁС¢Map¡¢Set)");
  579. console.log("8. ÕýÔò±í´ïʽ");
  580. console.log("9. JSON´¦Àí");
  581. console.log("10. ×Ö·û´®ºÍÈÕÆÚ´¦Àí");
  582. console.log("11. Éè¼ÆÄ£Ê½Ó¦Óà (¹¤³§¡¢¹Û²ìÕß¡¢×°ÊÎÆ÷)");
  583. console.log("\n×ܼÆ: 30¸ö¹¦ÄÜÄ£¿éÑÝʾÍê³É£¡");
  584. console.log("\n========== ÑÝʾ½áÊø ==========");
¸´ÖÆ´úÂë


»Ø¸´

ʹÓõÀ¾ß ¾Ù±¨

±¾°æ»ý·Ö¹æÔò

¹Ø±Õ

QQ|»ÓªÏúÈí¼þ×ÛºÏÌÖÂÛ|»ÓªÏúÈí¼þÓÐÎʱشð|»ÓªÏúÈí¼þ½Ì³Ì×¨Çø|»ÓªÏúÈí¼þPOST½Å±¾·ÖÏí|»ÓªÏúÈí¼þÆÕͨ½Å±¾·ÖÏí|»ÓªÏúÈí¼þÈí¼þ×ÊѶ|»ÓªÏúÈí¼þ¾«Æ·Èí¼þ|»ÓªÏúÈí¼þ¸üй«¸æ|ÓªÏúÈí¼þ|B2BÈí¼þ|B2BÍøÂçÈí¼þ ( ¾©ICP±¸09078825ºÅ )±¾ÍøÕ¾¿ª·¢µÄÓªÏúÈí¼þÊÇÒ»¿îеÄÍøÂçÓªÏúÈí¼þ£¬Õâ¿îÓªÏú¿ÉÒÔÈ¥ÍøÕ¾Èí¼þ£¬²©¿ÍÈí¼þ£¬B2BÈí¼þ£¬·ÖÀàÐÅÏ¢Íø·¢Ìù£¬¿ÉÒÔÇÀɳ·¢£¬¿ÉÒÔµ½°Ù¶ÈÎÄ¿âÉÏ´«WORDÎĵµ£¬¿ÉÒÔµ½Ò»Ð©ÊÇÏà²áÍøÕ¾×Ô¶¯ÉÏ´«Í¼Æ¬£¬Õâ¸ö×Ô¶¯·¢ÌûÈí¼þ×Ô´øÔÆÖ©Ö룬¼Ó¿ìÊÕ¼£¬ÓÐ6ÖÖ¶Ô½Ó´òÂë½Ó¿Ú£¬·½±ã£¬Ð§Âʸߣ¬Ëٶȿ죬¶øÇÒ¶ÔÍ϶¯µÄÑéÖ¤ÂëÈ«ÍøµÚÒ»¼Ò¶À¼ÒÖ§³Ö£¬È«²¿Ô­´´¼¼Êõ£¬¶À¼ÒÑз¢£¬Õý°æÔ­´´´ø°æÈ¨Èí¼þ¡£Ñ¡ÔñÍòÄÜÓªÏúÈí¼þ£¬¾ÍÑ¡ÔñÁËÒ»ÖÖ׬ǮµÄЧÂÊ£¬´ÓûÓб»³¬Ô½¹ý£¬Ò»Ö±ÔÚŬÁ¦Ñз¢Ð¼¼Êõ¡£·Å·ÉÃÎÏ룬½â·ÅË«ÊÖ£¬À´µã´´Ò⣬³É¾ÍÄãµÄÃÎÏ룬¾ÍÔÚÍòÄÜÓªÏúÈí¼þ¿ªÊ¼

map2

GMT+8, 2026-5-13 07:24 , Processed in 0.364054 second(s), 33 queries .

¿ìËٻظ´ ·µ»Ø¶¥²¿ ·µ»ØÁбí