Có rất nhiều bài viết, hướng dẫn tìm hiểu JavaScript nói chung và các kĩ thuật sử dụng JavaScript hiệu quả nói riêng trong nhiều năm qua. Nhưng tác giả của bài viết này tổng hợp lại 6 kĩ thuật JavaScript nâng cao mà bạn nên dùng khi làm việc với ngôn ngữ lập trình này, và đây cũng chỉ là ý kiến riêng của tác giả, bạn có thể thảo luận thêm về các kĩ thuật khác trong khu vực bình luận.
- Demo
- Phóng to
- Tải lại
- Cửa sổ mới
Tạo video bằng AI chỉ với giọng nói hoặc văn bản
Ứng dụng video AI MIỄN PHÍ hàng đầu của bạn! Tự động hóa video AI đầu tiên của bạn. Tạo Video Chuyên Nghiệp Của Bạn Trong 5 Phút Bằng AI Không Cần Thiết Bị Hoặc Kỹ Năng Chỉnh Sửa Video. Sản xuất video dễ dàng dành cho nhà tiếp thị nội dung.
There have been a number of articles published over the years that discuss best practices techniques for JavaScript. I thought I would go a little bit beyond the scope of those articles and outline a number of advanced techniques and practices that I have personally used or read about that could be invaluable in certain circumstances.
This article doesn't necessarily cover every detail of the methods I'm describing, but provides an overview, along with code examples, of some practical JavaScript coding techniques.
1. Closures to Extend Variable Scope
Closures in JavaScript are a fairly straightforward concept, and have been discussed online in a number of in-depth articles. The fact that they are straightforward doesn't necessarily mean they're simple however, as seen by the extensive articles that cover the subject.
Simply put, closures allow variable scope to be extended past the common scope restrictions of functions. I like the way Jeremy Keith describes closures in his book Bulletproof Ajax:
"Think of closures as a kind of regional scope: broader than local but not as broad as global."
To create a closure, you nest a function inside of a function. That inner function has access to all variables in its parent function's scope. This comes in handy when creating methods and properties in object oriented scripts. Here is a simple example that demonstrates the use of a closure:
function myObject() { this.property1 = "value1"; this.property2 = "value2"; var newValue = this.property1; this.performMethod = function() { myMethodValue = newValue; return myMethodValue; }; } var myObjectInstance = new myObject(); alert(myObjectInstance.performMethod());
The key portions of the script are the nested anonymous function are highlighted in green and the method call in the alert
function (last line). Because the method in the alert is actually
calling a nested function, that method is able to read the value of the
variable called newValue
, even thought that variable is not within the scope of the anonymous function, or method.
Developers use closures all the time, probably unknowingly, since a closure is created any time an anonymous function is nested inside another function and utilizes variables from the parent function's scope. The power of the closure is revealed when that method (the inner function) is called, and values that normally wouldn't be accessible are within "regional" scope and are thus able to be used as any other value.
See the references below for some deeper explanations of closures and their relation to scope. I also highly recommend you pick up a good advanced JavaScript book that offers a good discussion of the concepts associated with closures.
Further Reading
- Explaining JavaScript scope and closures (Robert Nyman)
- Closures in JavaScript (James Padolsey)
- JavasCript Closures at Jibbering.com
- JavaScript Closures for Dummies
2. Object Literals to Pass Optional Arguments
Here is a handy coding tip to keep in mind when dealing with functions that can accept a large number of optional arguments. Instead of passing the large number of arguments in the conventional fashion, which could unnecessarily complicate the function, you can pass just one argument which ends up being a collection of arguments declared in an object literal.
Let's look, first of all, at how we might do this in the typical manner, so we can see the contrast:
function showStatistics(name, team, position, average, homeruns, rbi) { document.write("<p><strong>Name:</strong> " + arguments[0] + "<br />"); document.write("<strong>Team:</strong> " + arguments[1] + "<br />"); if (typeof arguments[2] === "string") { document.write("<strong>Position:</strong> " + position + "<br />"); } if (typeof arguments[3] === "number") { document.write("<strong>Batting Average:</strong> " + average + "<br />"); } if (typeof arguments[4] === "number") { document.write("<strong>Home Runs:</strong> " + homeruns + "<br />"); } if (typeof arguments[5] === "number") { document.write("<strong>Runs Batted In:</strong> " + rbi + "</p>"); } } showStatistics("Mark Teixeira"); showStatistics("Mark Teixeira", "New York Yankees"); showStatistics("Mark Teixeira", "New York Yankees", "1st Base", .284, 32, 101);
The function above can take up to 6 arguments. The first two arguments are mandatory, so inside the function, we don't check for their existence. The last 4 arguments are not mandatory, so we only display their values if they exist.
We call the function 3 different times (last 3 lines), with different numbers of arguments each time. You can see that if the number of passed arguments was in the dozens, or more, the code could look a little messy, and would be harder to maintain, or read.
Now let's look at the same code using object literals to pass the arguments:
function showStatistics(args) { document.write("<p><strong>Name:</strong> " + args.name + "<br />"); document.write("<strong>Team:</strong> " + args.team + "<br />"); if (typeof args.position === "string") { document.write("<strong>Position:</strong> " + args.position + "<br />"); } if (typeof args.average === "number") { document.write("<strong>Average:</strong> " + args.average + "<br />"); } if (typeof args.homeruns === "number") { document.write("<strong>Home Runs:</strong> " + args.homeruns + "<br />"); } if (typeof args.rbi === "number") { document.write("<strong>Runs Batted In:</strong> " + args.rbi + "</p>"); } } showStatistics({ name: "Mark Teixeira" }); showStatistics({ name: "Mark Teixeira", team: "New York Yankees" }); showStatistics({ name: "Mark Teixeira", team: "New York Yankees", position: "1st Base", average: .284, homeruns: 32, rbi: 101 });
Technically, this second method of passing the arguments might require a little bit more code, but with a large collection of arguments, there are a few advantages.
First, the function itself is simplified because it accepts only one argument (args
), which is a collection of all the values passed from the object literal (name
, team
, position
,
etc). Plus, the actual argument values are easy to read, and can easily
be understood, updated, or modified, since the correlation between the
values and the argument references are more direct.
If the function required only a small number of arguments, then this method would not be necessary, and might actually have the opposite effect. So, use this technique sparingly, and only in situations where you foresee the collection of arguments being hard to maintain over time.
Further Reading
- JavaScript Object Literal
- JavaScript Object Literals Simplified
- JavaScript and Object Oriented Programming (OOP)
3. Contextual Targeting of DOM Elements
There are sometimes instances where you need to traverse the DOM and gain access to a specific element, or group of elements, but due to certain restrictions, you may not have direct access to the elements via a CSS class name or ID in the HTML code. This might be because of user-generated content produced through a rich text editor, or dynamic content pulled from a database.
Whatever the case, it's not impossible to access those unidentified DOM elements via JavaScript. Using what I call "contextual targeting", you can gain access to, and modify, almost any element in the DOM. As long as you have a map of the general template that contains the element you want to target, you can access that element and manipulate it the same way you would an element that has a class name or ID.
Let's create some basic HTML code that will serve as our example page:
<div id="header"> <h1>Site Title</h1> </div> <div id="sidebar"> <ul> <li><a href="/javascript/article/6_Advanced_JavaScript_Techniques_You_Should_Use.php/#">Testing</a></li> <li><a href="/javascript/article/6_Advanced_JavaScript_Techniques_You_Should_Use.php/#">Testing</a></li> <li><a href="/javascript/article/6_Advanced_JavaScript_Techniques_You_Should_Use.php/#">Testing</a></li> <li><a href="/javascript/article/6_Advanced_JavaScript_Techniques_You_Should_Use.php/#">Testing</a></li> <li><a href="/javascript/article/6_Advanced_JavaScript_Techniques_You_Should_Use.php/#">Testing</a></li> <li><a href="/javascript/article/6_Advanced_JavaScript_Techniques_You_Should_Use.php/#">Testing</a></li> </ul> </div> <div id="content"> <h2>Page Title</h2> <p><a href="/javascript/article/6_Advanced_JavaScript_Techniques_You_Should_Use.php/#">Lorum Ipsum link here</a>. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> <p><span style="color: red;">Pellentesque habitant morbi</span> tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> </div> <div id="footer"> <p>Copyright | <a href="/javascript/article/6_Advanced_JavaScript_Techniques_You_Should_Use.php/#">contact</a> | <a href="/javascript/article/6_Advanced_JavaScript_Techniques_You_Should_Use.php/#">policy</a> | <a href="/javascript/article/6_Advanced_JavaScript_Techniques_You_Should_Use.php/#">privacy</a></p> </div>
Using the HTML code above, if we wanted to target all the anchor tags on the page, we could collect them and manipulate them like this:
var myLinkCollection = document.getElementsByTagName("a"); for (i=0;i<myLinkCollection.length;i++) { // do something with the anchor tags here }
If we wanted to target only the anchor tags in the footer, however, we would target them based on their context, or surrounding elements, like this:
var myFooterElement = document.getElementById("footer"); var myLinksInFooter = myFooterElement.getElementsByTagName("a"); for (i=0;i<myLinksInFooter.length;i++) { // do something with footer anchor tags here }
The first line grabs a reference to the footer element. The second line collects all <a>
tags inside the footer. Then we loop through them and do what we want
with them. Thus, they are accessible even though they are not grouped
via class names.
You can accomplish the same thing by using node properties, as shown below.
var myLinkCollection = document.getElementsByTagName("a"); for (i=0;i<myLinkCollection.length;i++) { if (myLinkCollection[i].parentNode.parentNode.id === "footer") { // do something with footer anchor tags here } }
Similar code could be used to target the lone anchor tag inside the "content" section.
We could also limit our anchor tag search to include only tags that have the href
attribute set, so as to avoid finding any in-page links. We do this by using the getAttribute
method:
var myLinkCollection = document.getElementsByTagName("a"); for (i=0;i<myLinkCollection.length;i++) { if (myLinkCollection[i].getAttribute("href")) { // do something with the anchor tags here } }
Finally, you'll notice that there is a <span>
tag
with an inline style. The inline style could have been generated
through a content management system, so you may not have the ability to
edit it directly. You can target all <span>
elements with inline styles like this:
var myLinkCollection = document.getElementsByTagName("span"); for (i=0;i<myLinkCollection.length;i++) { if (myLinkCollection[i].getAttribute("style")) { // do something with all anchors that have inline styles } }
The possibilities are endless with contextual targeting, and there are even more options available if you're using a JavaScript library that normalizes browser differences and simplifies DOM manipulation.
Further Reading:
- Lượt gửi (0)
- Mới
Tạo video doanh nghiệp của bạn bằng AI chỉ với giọng nói hoặc văn bản
chatGPTaz.com
Nói chuyện với ChatGPT bằng ngôn ngữ mẹ đẻ của bạn
Ứng dụng AI Video
Ứng dụng video AI MIỄN PHÍ đầu tiên của bạn
Deepfake Video
Deepfake AI Video Maker
Deepfake
Deepfake AI Video Maker
AI Deep Fake
Deepfake AI Video Maker
AIvidio
AI Video Mobile Solutions
AIvideos
AI Video Platform & Solutions
AIvedio
AI Video App Maker
Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)
Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)
Faceswap AI trực tuyến
Đổi mặt Video, Ảnh & GIF ngay lập tức với Công cụ AI mạnh mẽ - Faceswap AI Trực tuyến MIỄN PHÍ
Faceswap AI trực tuyến
Đổi mặt Video, Ảnh & GIF ngay lập tức với Công cụ AI mạnh mẽ - Faceswap AI Trực tuyến MIỄN PHÍ
Faceswap AI trực tuyến
Đổi mặt Video, Ảnh & GIF ngay lập tức với Công cụ AI mạnh mẽ - Faceswap AI Trực tuyến MIỄN PHÍ
Powerful AI Presentation PPT Maker for FREE
Build an impressive presentation with our free online AI presentation app
Your next top AI Assistant
Claude AI, developed by Anthropic
Your next top AI Assistant
Claude AI, developed by Anthropic
Temu tặng $500 cho người dùng mới
Claim Free Temu $500 Credit via Affiliate & Influencer Program
Tín dụng quảng cáo TikTok miễn phí
Làm chủ quảng cáo TikTok cho hoạt động tiếp thị doanh nghiệp của bạn
Dall-E-OpenAI.com
Tự động tạo ra hình ảnh sáng tạo với AI
chatGPT4.win
Nói chuyện với ChatGPT bằng ngôn ngữ mẹ đẻ của bạn
Sản phẩm AI đầu tiên của Elon Musk - Grok/UN.com
Nói chuyện với Grok AI Chatbot bằng ngôn ngữ của bạn
Công cụ.win
Mở trung tâm công cụ miễn phí để mọi người sử dụng với hàng trăm công cụ
GateIO.gomymobi.com
Airdrop miễn phí để nhận, chia sẻ lên đến 150.000 đô la cho mỗi dự án
iPhoneKer.com
Tiết kiệm tới 630$ khi mua iPhone 16 mới
Mua Robot Tesla Optimus
Đặt mua Tesla Bot: Robot Optimus Gen 2 ngay hôm nay với giá dưới 20.000 đô la