{"id":1972,"date":"2024-10-09T11:37:53","date_gmt":"2024-10-09T03:37:53","guid":{"rendered":"https:\/\/www.openbyt.com\/spieth-in-danger-of-missing-cut-2"},"modified":"2024-10-19T21:30:38","modified_gmt":"2024-10-19T13:30:38","slug":"spieth-in-danger-of-missing-cut-2","status":"publish","type":"post","link":"https:\/\/www.openbyt.com\/zh\/\u65af\u76ae\u601d\u6709\u53ef\u80fd\u932f\u904e\u7b2c\u4e8c\u5834\u6bd4\u8cfd","title":{"rendered":"\u638c\u63e1 WordPress \u4e2d\u7684 JavaScript\uff1a\u6700\u4f73\u5be6\u52d9\u6307\u5357"},"content":{"rendered":"<p><strong>\u638c\u63e1 WordPress JavaScript \u7de8\u78bc\u6a19\u6e96\uff1a\u958b\u767c\u4eba\u54e1\u7d9c\u5408\u6307\u5357<\/strong><\/p><p>WordPress \u4e2d\u7684 JavaScript \u7de8\u78bc\u6a19\u6e96\u5c0d\u65bc\u7dad\u6301\u4e00\u81f4\u6027\u3001\u53ef\u8b80\u6027\u548c\u9ad8\u54c1\u8cea\u7684\u7a0b\u5f0f\u78bc\u81f3\u95dc\u91cd\u8981\uff0c\u4e0d\u8ad6\u60a8\u662f\u5728\u958b\u767c\u4e3b\u984c\u3001\u5916\u639b\uff0c\u6216\u662f\u70ba\u6838\u5fc3\u7a0b\u5f0f\u8ca2\u737b\u5fc3\u529b\u3002\u9019\u4e9b\u6a19\u6e96\u78ba\u4fdd\u60a8\u7684 JavaScript \u80fd\u8207 PHP\u3001HTML \u548c CSS \u7121\u7e2b\u6574\u5408\uff0c\u4e26\u4fc3\u9032\u4e0d\u540c\u5718\u968a\u4e4b\u9593\u7684\u5408\u4f5c\u3002\u8b93\u6211\u5011\u6df1\u5165\u77ad\u89e3\u9019\u4e9b\u6a19\u6e96\uff0c\u4ee5\u5be6\u969b\u7bc4\u4f8b\u5c07\u5176\u5206\u89e3\uff0c\u8b93\u6240\u6709\u958b\u767c\u4eba\u54e1\u90fd\u80fd\u7406\u89e3\u3002<\/p><figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.22.41-An-engaging-illustration-showing-the-importance-of-JavaScript-coding-standards-in-WordPress-development.-Include-visual-elements-representing-consiste.webp\" alt=\"\" class=\"wp-image-2286\" srcset=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.22.41-An-engaging-illustration-showing-the-importance-of-JavaScript-coding-standards-in-WordPress-development.-Include-visual-elements-representing-consiste.webp 1024w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.22.41-An-engaging-illustration-showing-the-importance-of-JavaScript-coding-standards-in-WordPress-development.-Include-visual-elements-representing-consiste-600x600.webp 600w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.22.41-An-engaging-illustration-showing-the-importance-of-JavaScript-coding-standards-in-WordPress-development.-Include-visual-elements-representing-consiste-100x100.webp 100w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.22.41-An-engaging-illustration-showing-the-importance-of-JavaScript-coding-standards-in-WordPress-development.-Include-visual-elements-representing-consiste-300x300.webp 300w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.22.41-An-engaging-illustration-showing-the-importance-of-JavaScript-coding-standards-in-WordPress-development.-Include-visual-elements-representing-consiste-150x150.webp 150w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.22.41-An-engaging-illustration-showing-the-importance-of-JavaScript-coding-standards-in-WordPress-development.-Include-visual-elements-representing-consiste-768x768.webp 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><p><strong>\u6982\u8ff0\uff1a\u5efa\u7acb\u5728 jQuery \u7684\u57fa\u790e\u4e0a<\/strong><\/p><p>WordPress JavaScript \u7de8\u78bc\u6a19\u6e96\u6e90\u81ea\u65bc <strong>jQuery JavaScript \u98a8\u683c\u6307\u5357<\/strong>\uff0c\u6700\u521d\u65bc 2012 \u5e74\u63a8\u51fa\u3002\u96d6\u7136\u6700\u521d\u662f\u91dd\u5c0d jQuery \u5c08\u6848\uff0c\u4f46\u5b83\u7684\u6210\u529f\u8b93\u6846\u67b6\u4ee5\u5916\u7684\u5c08\u6848\u4e5f\u5ee3\u6cdb\u63a1\u7528\u3002\u7136\u800c\uff0cWordPress \u5c0d\u9019\u4e9b\u6a19\u6e96\u4e5f\u6709\u81ea\u5df1\u7684\u770b\u6cd5\uff0c\u8207\u539f\u59cb\u7684 jQuery \u6307\u5357\u7565\u6709\u4e0d\u540c\u3002<\/p><figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.25.20-An-engaging-illustration-showing-the-evolution-of-JavaScript-coding-standards-in-WordPress-derived-from-the-jQuery-JavaScript-Style-Guide.-Include-vi.webp\" alt=\"\" class=\"wp-image-2287\" srcset=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.25.20-An-engaging-illustration-showing-the-evolution-of-JavaScript-coding-standards-in-WordPress-derived-from-the-jQuery-JavaScript-Style-Guide.-Include-vi.webp 1024w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.25.20-An-engaging-illustration-showing-the-evolution-of-JavaScript-coding-standards-in-WordPress-derived-from-the-jQuery-JavaScript-Style-Guide.-Include-vi-600x600.webp 600w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.25.20-An-engaging-illustration-showing-the-evolution-of-JavaScript-coding-standards-in-WordPress-derived-from-the-jQuery-JavaScript-Style-Guide.-Include-vi-100x100.webp 100w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.25.20-An-engaging-illustration-showing-the-evolution-of-JavaScript-coding-standards-in-WordPress-derived-from-the-jQuery-JavaScript-Style-Guide.-Include-vi-300x300.webp 300w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.25.20-An-engaging-illustration-showing-the-evolution-of-JavaScript-coding-standards-in-WordPress-derived-from-the-jQuery-JavaScript-Style-Guide.-Include-vi-150x150.webp 150w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.25.20-An-engaging-illustration-showing-the-evolution-of-JavaScript-coding-standards-in-WordPress-derived-from-the-jQuery-JavaScript-Style-Guide.-Include-vi-768x768.webp 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><p>\u4e3b\u8981\u5dee\u7570\u5305\u62ec<\/p><ol class=\"wp-block-list\"><li><strong>\u5b57\u7b26\u4e32\u7684\u55ae\u5f15\u865f<\/strong>:WordPress \u504f\u597d\u4f7f\u7528\u55ae\u5f15\u865f\u4f86\u8868\u793a\u5b57\u4e32\u8072\u660e\u3002<\/li>\n\n<li><strong>\u6848\u4f8b\u8aaa\u660e\u7e2e\u6392<\/strong>:\u5728 WordPress \u4e2d\uff0ccase \u8a9e\u53e5\u6703\u5728 switch \u5340\u584a\u4e2d\u7e2e\u6392\u3002<\/li>\n\n<li><strong>\u4e00\u81f4\u7684\u529f\u80fd\u7e2e\u6392<\/strong>:\u51fd\u6578\u5167\u7684\u6240\u6709\u5167\u5bb9\u90fd\u6703\u7e2e\u6392\uff0c\u5305\u62ec\u6a94\u6848\u7bc4\u570d\u5167\u7684\u5c01\u9589\u5305\u88dd\u7a0b\u5f0f\u3002<\/li>\n\n<li><strong>\u653e\u5bec\u7dda\u9577\u9650\u5236<\/strong>:jQuery \u5f37\u5236\u6bcf\u884c\u9650\u5236 100 \u500b\u5b57\u5143\uff0cWordPress \u5247\u9f13\u52f5\u4f46\u4e0d\u56b4\u683c\u57f7\u884c\u3002<\/li><\/ol><h3 class=\"wp-block-heading\">WordPress JavaScript \u6a19\u6e96\u6db5\u84cb\u7684\u6838\u5fc3\u9818\u57df<\/h3><p><strong>1.\u7a0b\u5f0f\u78bc\u683c\u5f0f\u8207\u7e2e\u6392<\/strong><\/p><ol class=\"wp-block-list\"><li><\/li><\/ol><p>\u6b63\u78ba\u7684\u683c\u5f0f\u548c\u7e2e\u6392\u5c0d\u65bc\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u7684\u7a0b\u5f0f\u78bc\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002WordPress \u6a19\u6e96\u5f37\u8abf\uff1a<\/p><ul class=\"wp-block-list\"><li><strong>\u58d3\u75d5<\/strong>:\u4f7f\u7528 <strong>\u6a19\u7c64<\/strong> \u800c\u4e0d\u662f\u7a7a\u683c\u4f5c\u70ba\u7e2e\u6392\u3002\u6bcf\u500b\u908f\u8f2f\u5340\u584a\u90fd\u61c9\u8a72\u7e2e\u6392\uff0c\u4ee5\u65b9\u4fbf\u95b1\u8b80\u3002\u5236\u8868\u7b26\u6709\u52a9\u65bc\u4fdd\u6301\u4e00\u81f4\u6027\uff0c\u5c24\u5176\u662f\u7576\u4e0d\u540c\u7684\u958b\u767c\u4eba\u54e1\u5728\u8655\u7406\u76f8\u540c\u7684\u7a0b\u5f0f\u78bc\u6642\u3002<\/li><\/ul><pre class=\"wp-block-code\"><code>( function ( $ ) {\n    \/\/ \u5340\u584a\u8868\u9054\u5f0f\u7e2e\u6392\n    function doSomething() {\n        \/\/ \u51fd\u5f0f\u4ee3\u78bc\u4e5f\u7e2e\u6392\n        console.log( 'Doing something' )\uff1b\n    }\n} )( jQuery )\uff1b<\/code><\/pre><p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u51fd\u6578 doSomething() \u53ca\u5176\u5167\u5bb9\u4ee5\u7e2e\u6392\u65b9\u5f0f\u986f\u793a\u5b83\u5011\u662f IIFE \u7684\u4e00\u90e8\u5206\u3002<\/p><p><strong>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u51fd\u6578 doSomething() \u53ca\u5176\u5167\u5bb9\u4ee5\u7e2e\u6392\u65b9\u5f0f\u986f\u793a\u5b83\u5011\u662f IIFE \u7684\u4e00\u90e8\u5206\u3002<\/strong><\/p><pre class=\"wp-block-code\"><code>var html = '&lt;p&gt;' + a + \u300c \u548c \u300d + b + \u300c \u7684\u7e3d\u548c\u662f \u300d + ( a + b ) + '&lt;\/p&gt;';\n\n\u5143\u7d20\n    .addClass( 'foo' )\n    .children()\n        .html( 'hello' )\n    .end()\n    .appendTo( 'body' )\uff1b<\/code><\/pre><p>\u5728\u6b64\uff0c\u93c8\u4e2d\u7684\u6bcf\u500b\u65b9\u6cd5\u90fd\u5f9e\u65b0\u7684\u4e00\u884c\u958b\u59cb\uff0c\u4f7f\u5f97\u64cd\u4f5c\u9806\u5e8f\u66f4\u6613\u65bc\u95b1\u8b80\u3002<\/p><figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.40.49-An-illustration-showing-key-differences-in-WordPress-JavaScript-coding-standards-including-single-quotes-for-strings-indentation-for-case-statements.webp\" alt=\"\" class=\"wp-image-2291\" srcset=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.40.49-An-illustration-showing-key-differences-in-WordPress-JavaScript-coding-standards-including-single-quotes-for-strings-indentation-for-case-statements.webp 1024w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.40.49-An-illustration-showing-key-differences-in-WordPress-JavaScript-coding-standards-including-single-quotes-for-strings-indentation-for-case-statements-600x600.webp 600w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.40.49-An-illustration-showing-key-differences-in-WordPress-JavaScript-coding-standards-including-single-quotes-for-strings-indentation-for-case-statements-100x100.webp 100w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.40.49-An-illustration-showing-key-differences-in-WordPress-JavaScript-coding-standards-including-single-quotes-for-strings-indentation-for-case-statements-300x300.webp 300w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.40.49-An-illustration-showing-key-differences-in-WordPress-JavaScript-coding-standards-including-single-quotes-for-strings-indentation-for-case-statements-150x150.webp 150w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.40.49-An-illustration-showing-key-differences-in-WordPress-JavaScript-coding-standards-including-single-quotes-for-strings-indentation-for-case-statements-768x768.webp 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><p><strong>2.\u7269\u4ef6\u8207\u9663\u5217\u9593\u8ddd<\/strong><\/p><p><strong>\u7269\u4ef6\u8207\u9663\u5217<\/strong>:\u4e00\u81f4\u7684\u9593\u8ddd\u662f\u8996\u89ba\u6e05\u6670\u5ea6\u7684\u95dc\u9375\uff0c\u5c24\u5176\u662f\u5728\u8655\u7406\u8907\u96dc\u7684\u8cc7\u6599\u7d50\u69cb\u6642\u3002\u8acb\u9075\u5faa\u4ee5\u4e0b\u9593\u8ddd\u6307\u5f15\uff1a<\/p><pre class=\"wp-block-code\"><code>\/\/ \u5ba3\u544a\u7269\u4ef6\u7684\u6b63\u78ba\u65b9\u6cd5\nvar obj = {\n    name: 'John\n    age: 27\u3001\n    height: 179\n};\n\n\/\/ \u4e0d\u6b63\u78ba\u7684\u7269\u4ef6\u5ba3\u544a\u65b9\u5f0f (\u4e0d\u8981\u6fc3\u7e2e\u5c6c\u6027)\nvar obj = { name: 'John', age: 27, height: 179 }\uff1b\n\n\/\/ \u9663\u5217\u4e5f\u61c9\u8a72\u9075\u5faa\u4e00\u81f4\u7684\u9593\u8ddd\nvar array = [ 1, 2, 3 ]\uff1b<\/code><\/pre><p>\u7269\u4ef6\u548c\u9663\u5217\u4e2d\u6b63\u78ba\u7684\u9593\u8ddd\u53ef\u78ba\u4fdd\u60a8\u7684\u8cc7\u6599\u7d50\u69cb\u5728\u8996\u89ba\u4e0a\u9bae\u660e\uff0c\u9019\u5728\u9664\u932f\u6216\u6aa2\u95b1\u7a0b\u5f0f\u78bc\u6642\u5f88\u6709\u5e6b\u52a9\u3002<\/p><p><strong>3.\u5206\u865f<\/strong><\/p><p>\u7e3d\u662f\u4f7f\u7528 <strong>\u5206\u865f<\/strong> \u4f86\u7d50\u675f\u8a9e\u53e5\u3002\u5728 JavaScript \u81ea\u52d5\u63d2\u5165\u5206\u865f (ASI) \u7684\u904e\u7a0b\u4e2d\uff0c\u7701\u7565\u5206\u865f\u53ef\u80fd\u6703\u5c0e\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u554f\u984c\u3002<\/p><pre class=\"wp-block-code\"><code>var array = [ 1, 2 ]\uff1b\nconsole.log( 'Hello, world!' )\uff1b<\/code><\/pre><p>\u5373\u4f7f JavaScript \u6709\u6642\u5019\u6703\u63a8\u65b7\u51fa\u5206\u865f\uff0c\u4f46\u6700\u597d\u7684\u505a\u6cd5\u9084\u662f\u660e\u78ba\u5730\u5305\u542b\u5206\u865f\uff0c\u4ee5\u907f\u514d\u6b67\u7fa9\uff0c\u5c24\u5176\u662f\u5728\u7d50\u5408\u591a\u500b\u7a0b\u5f0f\u78bc\u7247\u6bb5\u6216\u70ba\u5718\u968a\u5c08\u6848\u8ca2\u737b\u6642\u3002<\/p><p><strong>4.\u8b8a\u6578\u5ba3\u544a\uff1a <code>const<\/code>, <code>\u4efb<\/code>\u4ee5\u53ca <code>\u8b8a\u7570<\/code><\/strong><\/p><ul class=\"wp-block-list\"><li>\u4f7f\u7528 <strong>const<\/strong> \u503c\u4e0d\u6703\u6539\u8b8a\u7684\u8b8a\u6578\u3002\u9019\u6709\u52a9\u65bc\u9632\u6b62\u610f\u5916\u7684\u91cd\u65b0\u6307\u5b9a\uff0c\u4e26\u8b93\u5176\u4ed6\u958b\u767c\u4eba\u54e1\u6e05\u695a\u4e86\u89e3\u60a8\u7684\u610f\u5716\u3002<\/li>\n\n<li>\u4f7f\u7528 <strong>\u4efb<\/strong> \u9069\u7528\u65bc\u5176\u503c\u53ef\u80fd\u6703\u5728\u6307\u5b9a\u7bc4\u570d\u5167\u6539\u8b8a\u7684\u8b8a\u6578\u3002\u9019\u53ef\u78ba\u4fdd\u8b8a\u6578\u70ba\u5340\u584a\u4f5c\u7528\u57df\uff0c\u9632\u6b62\u8207\u61f8\u639b\u76f8\u95dc\u7684\u554f\u984c\u3002<\/li>\n\n<li>**\u907f\u514d **<strong>\u8b8a\u7570<\/strong> \u9664\u975e\u5fc5\u8981\uff0c\u56e0\u70ba\u5b83\u5177\u6709\u51fd\u6578\u7bc4\u570d\uff0c\u53ef\u80fd\u6703\u56e0\u70ba\u540a\u639b\u800c\u5c0e\u81f4\u975e\u9810\u671f\u7684\u884c\u70ba\u3002<\/li><\/ul><pre class=\"wp-block-code\"><code>const userName = 'Alice'; \/\/ \u5c0d\u56fa\u5b9a\u503c\u4f7f\u7528 const\nlet userAge = 30; \/\/ \u5c0d\u53ef\u80fd\u6539\u8b8a\u7684\u503c\u4f7f\u7528 let<\/code><\/pre><p>\u4f7f\u7528 <code>const<\/code> \u548c <code>\u4efb<\/code> \u9019\u6709\u52a9\u65bc\u6539\u5584\u7a0b\u5f0f\u78bc\u7684\u5b89\u5168\u6027\uff0c\u4e26\u8b93\u8b8a\u6578\u7684\u751f\u547d\u9031\u671f\u66f4\u5bb9\u6613\u63a8\u7406\u3002<\/p><p><strong>5.\u547d\u540d\u6163\u4f8b<\/strong><\/p><p>\u4e00\u81f4\u7684\u547d\u540d\u6163\u4f8b\u53ef\u8b93\u7a0b\u5f0f\u78bc\u66f4\u5177\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\uff1a<\/p><ul class=\"wp-block-list\"><li><strong>\u99f1\u99dd\u6bbc<\/strong>:\u4f7f\u7528 <strong>camelCase<\/strong> \u7528\u65bc\u8b8a\u6578\u548c\u51fd\u6578\u540d\u7a31\u3002\u4f8b\u5982 <code>\u4f7f\u7528\u8005 ID<\/code> \u6216 <code>\u53d6\u5f97\u4f7f\u7528\u8005\u8cc7\u6599<\/code>.<\/li>\n\n<li><strong>\u73ed\u7d1a<\/strong>:\u4f7f\u7528 <strong>\u5927\u5beb<\/strong> (PascalCase) \u8868\u793a\u985e\u5225\u540d\u7a31\u3002<\/li>\n\n<li><strong>\u5e38\u6578<\/strong>:\u4f7f\u7528 <strong>\u5c16\u53eb\u86c7\u7bb1<\/strong> \u70ba\u5e38\u6578\u3002<\/li><\/ul><pre class=\"wp-block-code\"><code>const MAX_CONNECTIONS = 5\uff1b\n\u985e\u5225 UserProfile {\n    constructor( name ) {\n        this.name = name\uff1b\n    }\n}<\/code><\/pre><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.52.16-An-educational-illustration-explaining-naming-conventions-in-WordPress-JavaScript-coding-standards.-Show-examples-for-camelCase-for-variable-and-func.webp\" alt=\"\" class=\"wp-image-2292\" srcset=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.52.16-An-educational-illustration-explaining-naming-conventions-in-WordPress-JavaScript-coding-standards.-Show-examples-for-camelCase-for-variable-and-func.webp 1024w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.52.16-An-educational-illustration-explaining-naming-conventions-in-WordPress-JavaScript-coding-standards.-Show-examples-for-camelCase-for-variable-and-func-600x600.webp 600w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.52.16-An-educational-illustration-explaining-naming-conventions-in-WordPress-JavaScript-coding-standards.-Show-examples-for-camelCase-for-variable-and-func-100x100.webp 100w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.52.16-An-educational-illustration-explaining-naming-conventions-in-WordPress-JavaScript-coding-standards.-Show-examples-for-camelCase-for-variable-and-func-300x300.webp 300w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.52.16-An-educational-illustration-explaining-naming-conventions-in-WordPress-JavaScript-coding-standards.-Show-examples-for-camelCase-for-variable-and-func-150x150.webp 150w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.52.16-An-educational-illustration-explaining-naming-conventions-in-WordPress-JavaScript-coding-standards.-Show-examples-for-camelCase-for-variable-and-func-768x768.webp 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><p>\u8b8a\u6578\u548c\u51fd\u6578\u7684 CamelCase \u6709\u52a9\u65bc\u5c07\u5b83\u5011\u8207\u985e\u5225\u548c\u5e38\u91cf\u5340\u5206\u958b\u4f86\uff0c\u4f7f\u4ee3\u78bc\u66f4\u6e05\u6670\u3002<\/p><p><strong>6.\u5e73\u7b49\u6aa2\u67e5<\/strong><\/p><p>\u7e3d\u662f\u4f7f\u7528 <strong>\u56b4\u683c\u76f8\u7b49\/\u4e0d\u76f8\u7b49 (===******** and !==)<\/strong> \u6aa2\u67e5\uff0c\u800c\u4e0d\u662f\u62bd\u8c61\u6aa2\u67e5 (== \u548c !=)\u3002\u9019\u6709\u52a9\u65bc\u907f\u514d\u53ef\u80fd\u5c0e\u81f4\u932f\u8aa4\u7684\u610f\u5916\u5f37\u5236\u985e\u578b\u3002<\/p><pre class=\"wp-block-code\"><code>if ( name === 'John' ) {\n    \/\/ \u56b4\u683c\u76f8\u7b49\u6aa2\u67e5\n    console.log( 'Hello, John!' )\uff1b\n}\n\nif ( result !== false ) { \/\/ \u56b4\u683c\u7684\u4e0d\u7b49\u865f\u6aa2\u67e5\u3002\n    \/\/ \u56b4\u683c\u4e0d\u7b49\u5f0f\u6aa2\u67e5\n    console.log( 'Result is not false' )\uff1b\n}<\/code><\/pre><p>\u56b4\u683c\u76f8\u7b49\u53ef\u78ba\u4fdd\u985e\u578b\u548c\u503c\u90fd\u6703\u88ab\u6bd4\u8f03\uff0c\u8b93\u60a8\u7684\u7a0b\u5f0f\u78bc\u66f4\u5177\u53ef\u9810\u6e2c\u6027\u3002<\/p><p><strong>7.\u5b57\u4e32\u8655\u7406<\/strong><\/p><p>\u4f7f\u7528 <strong>\u55ae\u5f15\u865f<\/strong> \u7528\u65bc\u5b57\u4e32\uff0c\u9664\u975e\u5b57\u4e32\u5305\u542b\u55ae\u5f15\u865f\uff0c\u5728\u9019\u7a2e\u60c5\u6cc1\u4e0b\u4f7f\u7528\u96d9\u5f15\u865f\u4ee5\u907f\u514d\u8f49\u7fa9\u3002<\/p><pre class=\"wp-block-code\"><code>var greeting = \"Hello world!\nvar statement = \"It's a nice day.\"\uff1b<\/code><\/pre><p>\u6b64\u898f\u5247\u53ef\u78ba\u4fdd\u6574\u500b\u7a0b\u5f0f\u78bc\u5eab\u7684\u4e00\u81f4\u6027\uff0c\u8b93\u958b\u767c\u4eba\u54e1\u66f4\u5bb9\u6613\u9075\u5faa\u76f8\u540c\u7684\u4f5c\u6cd5\u3002<\/p><p><strong>8.\u5207\u63db\u8a9e\u53e5<\/strong><\/p><p>\u5207\u63db\u8a9e\u53e5\u61c9\u8a72\u6709\u4e00\u500b <strong>\u6253\u7834<\/strong> \u6bcf\u7a2e\u60c5\u6cc1\uff08\u9ed8\u8a8d\u9664\u5916\uff09\uff0c\u4ee5\u9632\u6b62\u58ae\u843d\u932f\u8aa4\u3002\u6b64\u5916\u3001 <strong>\u7e2e\u6392 case \u8a9e\u53e5<\/strong> \u61c9\u8a72\u5728\u958b\u95dc\u5167\u7684\u4e00\u500b\u7d22\u5f15\u6a19\u7c64\u4e2d\u3002<\/p><pre class=\"wp-block-code\"><code>switch ( event.keyCode ) {\n    case $.ui.keyCode.ENTER\uff1a\n    case $.ui.keyCode.SPACE\uff1a\n        executeFunction()\uff1b\n        break\uff1b\n    case $.ui.keyCode.ESCAPE\uff1a\n        cancelFunction()\uff1b\n        break\uff1b\n    \u9810\u8a2d\uff1a\n        defaultFunction()\uff1b\n}<\/code><\/pre><p>\u9069\u7576\u7684\u7e2e\u6392\u548c\u4f7f\u7528\u5206\u9694\u7b26\u53ef\u907f\u514d\u5728\u7b26\u5408\u591a\u7a2e\u60c5\u6cc1\u6642\u767c\u751f\u975e\u9810\u671f\u7684\u884c\u70ba\u3002<\/p><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.59.17-An-educational-illustration-summarizing-WordPress-JavaScript-coding-standards-including-strict-equality-checks-string-handling-and-switch-statement-.webp\" alt=\"\" class=\"wp-image-2293\" srcset=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.59.17-An-educational-illustration-summarizing-WordPress-JavaScript-coding-standards-including-strict-equality-checks-string-handling-and-switch-statement-.webp 1024w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.59.17-An-educational-illustration-summarizing-WordPress-JavaScript-coding-standards-including-strict-equality-checks-string-handling-and-switch-statement--600x600.webp 600w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.59.17-An-educational-illustration-summarizing-WordPress-JavaScript-coding-standards-including-strict-equality-checks-string-handling-and-switch-statement--100x100.webp 100w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.59.17-An-educational-illustration-summarizing-WordPress-JavaScript-coding-standards-including-strict-equality-checks-string-handling-and-switch-statement--300x300.webp 300w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.59.17-An-educational-illustration-summarizing-WordPress-JavaScript-coding-standards-including-strict-equality-checks-string-handling-and-switch-statement--150x150.webp 150w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-20.59.17-An-educational-illustration-summarizing-WordPress-JavaScript-coding-standards-including-strict-equality-checks-string-handling-and-switch-statement--768x768.webp 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><p><strong>WordPress JavaScript \u958b\u767c\u7684\u6700\u4f73\u5be6\u52d9<\/strong><\/p><h4 class=\"wp-block-heading\">1.\u907f\u514d\u4f7f\u7528\u5168\u5c40\u8b8a\u6578<\/h4><p>\u5168\u5c40\u8b8a\u6578\u6703\u5c0e\u81f4 <strong>\u547d\u540d\u7a7a\u9593\u6c61\u67d3<\/strong> \u4ee5\u53ca\u8207\u5176\u4ed6\u8173\u672c\u885d\u7a81\u3002\u53cd\u4e4b\uff0c\u8acb\u5c07\u60a8\u7684\u7a0b\u5f0f\u78bc\u5c01\u88dd\u5728 <strong>\u7acb\u5373\u555f\u7528\u51fd\u5f0f\u8868\u9054 (IIFE)<\/strong> \u4f86\u5efa\u7acb\u5c40\u90e8\u7bc4\u570d\u3002<\/p><pre class=\"wp-block-code\"><code>( function() {\n    const localVar = 'Scoped to this function'\uff1b\n    \/\/ \u6b64\u8655\u7684\u7a0b\u5f0f\u78bc\u53d7\u5168\u57df\u4fdd\u8b77\n} )();<\/code><\/pre><p>\u5c01\u88dd\u7a0b\u5f0f\u78bc\u53ef\u964d\u4f4e\u885d\u7a81\u7684\u98a8\u96aa\uff0c\u5c24\u5176\u662f\u5728\u4f7f\u7528\u591a\u500b\u7b2c\u4e09\u65b9\u7a0b\u5f0f\u5eab\u7684\u74b0\u5883\u4e2d\u5de5\u4f5c\u6642\u3002<\/p><p><strong>2.\u6587\u4ef6\u8207\u8a55\u8ad6<\/strong><\/p><p>WordPress \u9075\u5faa <strong>JSDoc 3 \u6a19\u6e96<\/strong> \u4f86\u8a18\u9304 JavaScript \u7a0b\u5f0f\u78bc\u3002\u6587\u4ef6\u5c0d\u65bc\u7406\u89e3\u8907\u96dc\u7684\u65b9\u6cd5\u3001\u985e\u5225\u548c\u51fd\u5f0f\u7684\u529f\u80fd\u81f3\u95dc\u91cd\u8981\u3002<\/p><ul class=\"wp-block-list\"><li><strong>\u55ae\u884c\u8a3b\u89e3<\/strong>:\u4f7f\u7528 <code>\/\/<\/code> \u9032\u884c\u7c21\u77ed\u8aaa\u660e\uff0c\u5c24\u5176\u662f\u5728\u63cf\u8ff0\u7279\u5b9a\u7684\u7a0b\u5f0f\u78bc\u884c\u6642\u3002<\/li>\n\n<li><strong>\u591a\u884c\u8a3b\u89e3<\/strong>:\u4f7f\u7528 <code>\/* *\/<\/code> \u4ee5\u7372\u5f97\u66f4\u8a73\u7d30\u7684\u89e3\u91cb\u6216\u63cf\u8ff0\u7a0b\u5f0f\u78bc\u5340\u584a\u3002<\/li><\/ul><pre class=\"wp-block-code\"><code>\/**\n * \u5c07\u5169\u500b\u6578\u5b57\u76f8\u52a0\u3002\n *\n * @param {number} a - \u7b2c\u4e00\u500b\u6578\u5b57\u3002\n * @param {number} b - \u7b2c\u4e8c\u500b\u6578\u5b57\u3002\n * @return {number} a \u548c b \u7684\u7e3d\u548c\u3002\n *\/\nfunction add( a, b ) {\n    return a + b\uff1b\n}<\/code><\/pre><p>\u4f7f\u7528 JSDoc \u8a18\u9304\u7a0b\u5f0f\u78bc\u53ef\u8b93\u5de5\u5177\u81ea\u52d5\u7522\u751f\u6587\u4ef6\uff0c\u4e26\u5354\u52a9\u958b\u767c\u4eba\u54e1\u77ad\u89e3\u60a8\u7684\u7a0b\u5f0f\u78bc\uff0c\u800c\u7121\u9700\u6df1\u5165\u5be6\u4f5c\u3002<\/p><p><strong>3.\u932f\u8aa4\u8655\u7406<\/strong><\/p><p>\u4f7f\u7528 <strong>try...catch<\/strong> \u5340\u584a\u4f86\u512a\u96c5\u5730\u8655\u7406\u6f5b\u5728\u7684\u932f\u8aa4\u3002\u932f\u8aa4\u8655\u7406\u53ef\u78ba\u4fdd\u610f\u5916\u554f\u984c\u4e0d\u6703\u5c0e\u81f4\u6574\u500b\u61c9\u7528\u7a0b\u5f0f\u5931\u6557\u3002<\/p><pre class=\"wp-block-code\"><code>try {\n    const data = JSON.parse( jsonString )\uff1b\n} catch ( \u932f\u8aa4 ) {\n    console.error( 'Invalid JSON:', error )\uff1b\n}<\/code><\/pre><p>\u9069\u7576\u7684\u932f\u8aa4\u8655\u7406\u53ef\u63d0\u9ad8\u7a0b\u5f0f\u78bc\u7684\u5f48\u6027\uff0c\u4f7f\u5176\u66f4\u5bb9\u6613\u9664\u932f\u548c\u7dad\u8b77\u3002<\/p><h3 class=\"wp-block-heading\">JavaScript \u7de8\u78bc\u6a19\u6e96\u5be6\u52d9<\/h3><p>\u78ba\u4fdd\u60a8\u7684 JavaScript \u7a0b\u5f0f\u78bc\u7b26\u5408 WordPress \u6a19\u6e96<\/p><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-21.06.47-A-concluding-illustration-for-an-article-on-WordPress-JavaScript-coding-standards.-Depict-developers-working-together-applying-best-practices-and-wr.webp\" alt=\"\" class=\"wp-image-2294\" srcset=\"https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-21.06.47-A-concluding-illustration-for-an-article-on-WordPress-JavaScript-coding-standards.-Depict-developers-working-together-applying-best-practices-and-wr.webp 1024w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-21.06.47-A-concluding-illustration-for-an-article-on-WordPress-JavaScript-coding-standards.-Depict-developers-working-together-applying-best-practices-and-wr-600x600.webp 600w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-21.06.47-A-concluding-illustration-for-an-article-on-WordPress-JavaScript-coding-standards.-Depict-developers-working-together-applying-best-practices-and-wr-100x100.webp 100w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-21.06.47-A-concluding-illustration-for-an-article-on-WordPress-JavaScript-coding-standards.-Depict-developers-working-together-applying-best-practices-and-wr-300x300.webp 300w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-21.06.47-A-concluding-illustration-for-an-article-on-WordPress-JavaScript-coding-standards.-Depict-developers-working-together-applying-best-practices-and-wr-150x150.webp 150w, https:\/\/www.openbyt.com\/wp-content\/uploads\/2024\/10\/DALL\u00b7E-2024-10-10-21.06.47-A-concluding-illustration-for-an-article-on-WordPress-JavaScript-coding-standards.-Depict-developers-working-together-applying-best-practices-and-wr-768x768.webp 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>","protected":false},"excerpt":{"rendered":"<p>\u638c\u63e1 WordPress JavaScript \u7de8\u78bc\u6a19\u6e96\uff1aWordPress \u4e2d\u7684 JavaScript \u7de8\u78bc\u6a19\u6e96\u5c0d\u65bc\u7dad\u6301\u7a0b\u5f0f\u78bc\u7684\u4e00\u81f4\u6027\u3001\u53ef\u8b80\u6027\u548c\u9ad8\u54c1\u8cea\u81f3\u95dc\u91cd\u8981\uff0c\u7121\u8ad6...<\/p>","protected":false},"author":1,"featured_media":3129,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36,83],"tags":[196,195,197,194],"class_list":["post-1972","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev-tips","category-developer-tips","tag-best-practices","tag-coding-standards","tag-web-development","tag-wordpress-javascript",""],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/posts\/1972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/comments?post=1972"}],"version-history":[{"count":6,"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/posts\/1972\/revisions"}],"predecessor-version":[{"id":3542,"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/posts\/1972\/revisions\/3542"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/media\/3129"}],"wp:attachment":[{"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/media?parent=1972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/categories?post=1972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.openbyt.com\/zh\/wp-json\/wp\/v2\/tags?post=1972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}