


Example tutorial on overriding jQuery object methods in JavaScript_jquery
jQuery is a very excellent class library. It solves a lot of client-side programming for us, but nothing is omnipotent. When it cannot meet our needs, we need to rewrite it. At the same time Do not affect or modify its original functions; for example, most of the data interaction in my current web application is completed through Ajax, so that some hidden field data can be saved in HTML tags In the attributes, the amount of code in HTML tags is reduced, such as: ID, Timestamp, etc. These fields that do not require user input but have to be submitted are submitted through the form
<input name="ID" value="343" type="hidden" />
Save the ID value in a hidden tag and submit it with the form.
The code is as follows:
<div> <label data-field="id" data-property="data-id" data-id="343">First Name</label><input type="text" data-field="FirstName" /> </div>
Please don’t pay too much attention to the blue part of this attribute name. You can choose some more concise names. Now let’s rewrite jQuery’s val method to read and set the value of data-id to $.prototype. val redefines a function and passes in the base class function in the form of a closure so that it can be called in the new function. See the following code:
<script> $.prototype.val = function (base) { return function () { var s = this, a = "data-property", p = s.attr(a), isset = arguments.length > 0, v = isset ? arguments[0] : null; //这里调用基类方法,当然基类方法在何时调用或者是否要调用取决于您的业务逻辑,在这里我们是要调用的,因为要保持它原有的功能。 if (isset&&typeof(base)=="function") { base.call(s, v); } else { v = base.call(s); } if (p) { if (isset) { s.attr(p, v); return s } else { return s.attr(p) } } else { if (!s.is(":input")) { if (isset) { s.text(v); return s; } else { return s.text(); } } else { return isset ? s : v; } } } //在这里传入基类方法 }($.prototype.val); </script>
After this rewrite, when the data-property attribute is specified in the tag, calling val() on the jQuery object is equivalent to calling attr("data-property"), but no data-property is specified, which is the default , if it is a non-form element, val() is equivalent to text(), if it is a form element, the original function is maintained, which is to read and write the value of the value attribute, so you can use this method: $("[data-field= 'id']").val(345) and $("[data-field='id']").val() read or set its value, the "data-field" attribute will be mapped to In the fields of corresponding types on the server, this is the method of rewriting jQuery in JavaScript. The rewriting of other methods has the same effect, so you can draw inferences from one example.
All codes are as follows:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>在JavaScript中重写对象的方法</title> <script src="Scripts/jquery-1.8.2.min.js"></script> <script src="Scripts/jquery-ui-1.8.24.min.js"></script> <script> $.prototype.val = function (base) { return function () { var s = this, a = "data-property", p = s.attr(a), isset = arguments.length > 0, v = isset ? arguments[0] : null; if (isset&&typeof(base)=="function") { base.call(s, v); } else { v = base.call(s); } if (p) { if (isset) { s.attr(p, v); return s } else { return s.attr(p) } } else { if (!s.is(":input")) { if (isset) { s.text(v); return s; } else { return s.text(); } } else { return isset ? s : v; } } } }($.prototype.val); </script> </head> <body> <span id="lbl">Hello world!</span> <input type="text" id="txt" value="hello world" /> <input type="checkbox" value="哈哈哈。。。" /> </body> </html>
I hope this article will be helpful to everyone’s web front-end design.

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor
