Javascript: Prototype attribute usage instructions

Source: Internet
Author: User
Prototype is a method introduced in IE 4 and later versions for a certain type of objects, and it is particularly convenient: it is a method for adding methods to class objects! This may sound a little messy. Don't worry. Next I will explain this special method through the instance: first, we need to first understand the concept of the class, javascript itself is an object-oriented language. The elements involved in Javascript depend on a specific class according to their attributes. Common classes include: array, Boolean, date, function, and number) object, string, and so on. Program Frequently Used by members (here we need to distinguish between class attention and attribute sending methods), such as the push method of the array, the get method of the date series, and the split method of the string, but in the actual programming process, I wonder if I feel the shortcomings of the existing method? The prototype method came into being! The following describes how to use prototype from a simple perspective: 1. The simplest example shows prototype :( 1) number. add (Num): function, Number Addition implementation method: Number. prototype. add = function (Num) {return (This + num);} test: Alert (3 ). add (15)-> show 18 (2) Boolean. rev (): function. boolean variable inversion implementation method: Boolean. prototype. REV = function () {return (! This);} test: Alert (true). Rev ()-> is it easy to show false? This section only tells readers that this method is used in this way. 2. Implementation and enhancement of existing methods: (1) array. push (new_element): Add a new element at the end of the array implementation method: array. prototype. push = function (new_element) {This [this. length] = new_element; return this. length;} Let's further enhance him so that he can add multiple elements at a time! Implementation Method: array. prototype. pushpro = function () {var currentlength = This. length; For (VAR I = 0; I <arguments. length; I ++) {This [currentlength + I] = arguments [I];} return this. length;} Should be difficult to understand? And so on, you can consider how to enhance array. Pop to delete any location and any number of elements ( Code (2) string. length: this is actually an attribute of the string class. However, because JavaScript regards full and half-width as a character, it may cause some problems in some practical applications, now we use prototype to make up for this deficiency. Implementation Method: string. prototype. cnlength = function () {var arr = This. match (/[^ \ x00-\ xFF]/ig); return this. length + (ARR = NULL? 0: arr. length);} test: Alert ("easewe space spaces ". cnlength ()-> show 16 here some regular expression methods and full-angle character encoding principles are used. because they belong to the other two relatively large classes, this article does not describe them, see related materials. 3. Implementation of new functions, in-depth prototype: in actual programming, it is certainly not only the enhancement of existing methods, but also more functional requirements, the following are two examples of solving the actual problem using prototype: (1) string. left () Problem: Anyone who has used VB should know the left function and take n characters from the left side of the string. However, the full and half corners are considered as one character, in this case, you cannot intercept long strings in the layout of a mix of Chinese and English characters. n characters are truncated from the left side of the string and the full-width and half-width characters are supported. prototype. left = function (Num, mode) {If (! /\ D +/. Test (Num) Return (this); var STR = This. substr (0, num); If (! Mode) return STR; var n = Str. tlength ()-Str. length; num = num-parseint (n/2); return this. substr (0, num);} test: Alert ("easewe space spaces ". left (8)-> show easewe space alert ("easewe space spaces ". left (8, true)-> show easewe null this method uses the string mentioned above. the tlength () method can also combine some good new methods between custom methods! (2) date. daydiff (): Calculate the interval (year, month, day, week) of two date variables. Implementation Method: Date. prototype. daydiff = function (cdate, mode) {try {cdate. getyear () ;}catch (e) {return (0) ;}var base = 60*60*24*1000; var result = math. ABS (this-cdate); Switch (mode) {Case "Y": result/= base * 365; break; Case "M": result/= base * 365/12; break; case "W": result/= base * 7; break; default: result/= base; break;} return (math. floor (result);} test: Alert (n EW date ()). daydiff (new date (329, 1)-> Display alert (new date ()). daydiff (new date (, 1), "M")-> display 10. Of course, you can expand it to get the response hour, minute, or even second. (3) number. FACT (): number. prototype. fact = function () {var num = math. floor (this); If (Num <0) return Nan; If (num = 0 | num = 1) return 1; else return (Num * (num-1 ). FACT ();} test: Alert (4 ). FACT ()-> display 24 This method mainly shows that the recursive method is also feasible in the prototype method!

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.