Prototype attribute of JavaSript

Source: Internet
Author: User

I recently found that the method for deleting a node was not found during the Array operation on JavaSript, But I need to call the method for deleting a node continuously to achieve the effect, after finding relevant information, we can use the prototype attribute to add a method to the Array to delete nodes. This method is the same as the push (), pop (), reverse (), slice () Methods of the Array (), sort (), join () and other methods are the same. Code 1 [javascript] Array. prototype. remove = function (dx) {if (isNaN (dx) | dx> this. length) {return false;} for (var I = 0, n = 0; I <this. length; I ++) {if (this [I]! = This [dx]) {this [n ++] = this [I]} this. length-= 1} test the method code 2 [javascript] $ (function () {var arr = [1, 2, 3]; arr. remove (1); var s = ''; for (var I = 0; I <arr. length; I ++) {s = s + arr [I] + ',';} alert (s. substring (0, s. length-1) ;}; the array is numbered from 0, so the deleted element is "2" and the output result is "1, 3", which is correct. W3school explains that prototype enables you to add attributes and methods to objects. Because JavaSript does not have the concept of a class, it cannot inherit from an object-oriented language. Prototype opens a door for developers to solve this problem. Through this door, developers can define attributes and methods on objects, and then initialize a variable, such as an array, the Array has a reference to the newly defined attributes and methods (it can be understood as an "inheritance" of the newly defined attributes and methods of the Array object "). Some people will regard the code in "code 1" as a value assignment, which is extremely inaccurate. If it is a value assignment, it will point to the same area of memory. When we Initialize an array in Code 2, array 1 and array 2 call the remove () method respectively, will not be affected. The Format () method is implemented in the Date object, which is usually used more. [Javascript] // implement the Format method on the Date object, convert Date to the String // month (M), Day (d), hour (h) in the specified Format), minute (m), second (s), quarter (q) can use 1-2 placeholders, // year (y) can use 1-4 placeholders, only one placeholder (one to three digits) can be used in milliseconds (S). // usage: // (new Date ()). format ("yyyy-MM-dd hh: mm: ss. S ")-> 10:12:30. 526 // (new Date ()). format ("yyyy-M-d h: m: s. S ")->. 25 Date. prototype. format = function (fmt) {var o = {"M +": this. getMonth () + 1, // month "d +": this. getD Ate (), // Day "h +": this. getHours (), // Hour "m +": this. getMinutes (), // minute "s +": this. getSeconds (), // second "q +": Math. floor (this. getMonth () + 3)/3), // quarter "S": this. getMilliseconds () // millisecond}; // RegExp. $1 returns the matched first string (identified by parentheses) if (/(y + )/. test (fmt) fmt = fmt. replace (RegExp. $1, (this. getFullYear () + ""). substr (4-RegExp. $1. length); for (var k in o) {if (new RegExp ("(" + k + ")"). test (fmt) fmt = fmt. replac E (RegExp. $1, (RegExp. $1. length = 1 )? (O [k]): ("00" + o [k]). substr ("" + o [k]). length);} return fmt;} in this example, there is a regular expression application, RegExp. $1 returns the first matched string. The following example is used to illustrate the problem. [Javascript] $ (function () {var r =/^ (\ d {4})-(\ d {1, 2})-(\ d {1, 2}) $ /; r.exe c ('1970-01-10 '); s1 = RegExp. $1; s2 = RegExp. $2; s3 = RegExp. $3; alert (s1 + "," + s2 + "," + s3)}); returned results, 10 This article mainly summarizes the experiences of using the prototype attribute of JavaSript. We get used to it too often during program development. this operator is used to introduce the built-in attributes and methods of objects. However, sometimes others are not prepared for us as we expected, the prototype attribute allows us to customize methods and attributes on objects.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.