Home >Web Front-end >JS Tutorial >A brief analysis of how to use prototype to define your own methods_Basic knowledge
Prototype is a method introduced in IE 4 and later versions for objects of a certain class, and the special thing is: it is a method for adding methods to objects of a class! This may sound a bit confusing, don’t worry, I will explain this special method through examples below:
First of all, we need to understand the concept of classes. JavaScript itself is an object-oriented language, and the elements involved are attached to a specific class based on their attributes. Our common classes include: array variables (Array), logical variables (Boolean), date variables (Date), structure variables (Function), numerical variables (Number), object variables (Object), string variables (String), etc. , and related class methods are also often used by programmers (here we need to distinguish between class attention and attribute sending methods), such as the push method of the array, the get series method of the date, the split method of the string, etc.,
But in the actual programming process, do you feel the shortcomings of the existing methods? The prototype method came into being! Below, we will explain the specific use of prototype from shallow to deep through examples:
1. The simplest example to understand prototype:
(1) Number.add(num): Function, adding numbers
Implementation method: Number.prototype.add = function(num){return(this num);}
Test: alert((3).add(15)) -> Show 18
(2) Boolean.rev(): Function, invert Boolean variable
Implementation method: Boolean.prototype.rev = function(){return(!this);}
Test: alert((true).rev()) -> show false
Isn’t it very simple? This section just tells readers about another method and how this method is used.
2. Implementation and enhancement of existing methods, first introduction to prototype:
(1) Array.push(new_element)
Function: Add a new element to the end of the array
Implementation method:
Implementation method:
(2) String.length
Function: This is actually an attribute of the String class, but since JavaScript treats both full-width and half-width as one character, it may not work in some practical applications. It will cause certain problems. Now we use prototype to make up for this shortcoming.
Implementation method:
Some regular expression methods and the encoding principles of full-width characters are used here. Since they belong to two other larger categories, this article will not explain them. Please refer to relevant materials.
3. Implementation of new functions, in-depth prototype: What is used in actual programming is definitely not only the enhancement of existing methods, but also more functional requirements. Below I will give two examples of using prototypes to solve practical problems:
(1) String.left()
Problem: Anyone who has used vb should know the left function, which takes n characters from the left side of the string, but the disadvantage is that both full-width and half-width are considered Because it is a character, it is impossible to intercept strings of equal length in a layout with mixed Chinese and English
Function: intercept n characters from the left side of the string, and support the distinction between full-width and half-width characters
Implementation method:
Function: Calculate the interval (year, month, day, week) between two date variables
Implementation method:
Copy code
The code is as follows: