How to quickly get started with VUE3.0: Enter learning
Related recommendations: JavaScript tutorial
What is constructor: It is another way to create objects in JavaScript.
Contrast with creating objects using literal methods: Constructors can create some objects with the same characteristics.
Example: Create apple, banana, and orange objects through the fruit constructor. Its characteristic is that these objects are all created based on the same template, and each object has its own characteristics.
The characteristics and advantages of creating objects using literal methods
: simplicity and flexibility.
Disadvantage: When you need to create a set of objects with the same characteristics, you cannot specify through code which members these objects should have in common.
The way to implement templates in object-oriented programming languages is to use classes to create templates and implement different objects (instances of classes) based on the templates.
Method 1 for implementing templates in JavaScript: through a factory function, which creates an object using the literal "{ }" inside it. The disadvantage is that the type of the object cannot be distinguished.
Way 2 to implement templates in JavaScript: create objects through constructors.
Before learning how to customize the constructor, let's first take a look at how to use the JavaScript's built-in constructor.
: How to customize constructors?
Note that
when learning JavaScript, beginners are often confused by some similar terms, such as function, method, constructor, constructor, constructor, etc.
In fact, they can all be collectively called functions, but they have different names in different usage scenarios. By convention, functions defined in an object are called methods of the object.
As for the constructor, some people are accustomed to calling it a constructor or a constructor. We only need to understand that these names refer to the same thing.
The reason whywas not available before ES6: to simplify the difficulty.
Reason for adding: With the development of Web front-end technology, some people who were originally engaged in back-end development turned to the front-end. In order to make JavaScript closer to the syntax of some back-end languages so that developers can adapt faster.
The role of the class keyword: used to define a class.
Features: The constructor constructor method can be defined in the class.
Note that
the class syntax is essentially syntactic sugar, and is only designed to facilitate user use. The same effect can be achieved without using this syntax, such as the constructor learned earlier. In case the user's browser does not support this syntax, this method is not recommended.
Concept of: In the constructor, variables defined using the var keyword are called private members.
Features: After the instance object, it cannot be accessed through "object.member", but private members can be accessed in the member method of the object.
Features: The private member name reflects object-oriented encapsulation.
This is because these objects are actually instances of the constructor String, i.e. String objects.
Note
that when operating on a string, the processing result is returned directly through the return value of the method and does not change the string content stored in the String object itself. In the parameters of these methods, the position is an index value, starting from 0, the index value of the first character is 0, and the index value of the last character is the length of the string minus 1.
Take the example of limiting the length of user names to 3 to 10 and not allowing the sensitive word admin to be used for demonstration.
Number object is used to process integers, floating point numbers and other numerical values. Commonly used properties and methods are as follows.
It is a static member of Number and is accessed directly through the constructor Number, not an instance of Number.
The Math object is used to perform mathematical operations on numerical values. Unlike other objects, this object is not a constructor and does not need to be instantiated to be used.
Take Math.random() to obtain random numbers within a specified range as an example.
The formula is Math.random() * (n - m) + m, which means generating a random value greater than or equal to m and less than n
The Date object is used to handle dates and times.
Example 1: Get the time and date based on the Date object.
Example 2: Specify a date based on a Date object.
Example 3: Handle the situation where the set date is unreasonable. For example, setting the month to -1 means December last year, and setting the month 12 means January next year.
Code implementation idea:
Code implementation
<html> <head><title>This month’s calendar</title> </head> <body> <p align=center><b>This month’s calendar</b></p> <script> var thisyear,thismonth,today=new Date();; thisyear=today.getFullYear() thismonth=today.getMonth(); var imonth,iweekday,iday,nextday; document.write("<table align=center border=1><tr align=center bgcolor=#fff00>") document.write("<td>Sunday</td><td>Monday</td><td>Tuesday</td><td>Wednesday</td><td>Thursday</td><td> Friday</td><td>Saturday</td>") document.write("</tr><tr>") nextday=1; var thisdate=new Date(thisyear,thismonth,nextday) for (iday=0;iday<=6;iday++){ if (thisdate.getDay() > iday) { document.write("<td>"); document.write("</td>") } else { if (thisdate.getMonth()== today.getMonth()&&thisdate.getDate()== today.getDate() &&thisdate.getFullYear()== today.getFullYear() ){ document.write("<td><font color=red><b>") document.write(nextday) document.write("</font></b></td>") } else { document.write("<td><b>"); document.write(nextday); document.write("</b></td>"); } nextday=nextday+1; thisdate.setDate(nextday); } } document.write("</tr>"); document.write("<tr>") iweekday=1 while(thisdate.getMonth() == thismonth ){ if (thisdate.getMonth()== today.getMonth()&&thisdate.getDate()== today.getDate() &&thisdate.getFullYear()== today.getFullYear() ){ document.write("<td><font color=red><b>") document.write(nextday) document.write("</b></font></td>") } else{ document.write("<td><b>") document.write(nextday) document.write("</b></td>") } nextday=nextday+1; iweekday=iweekday+1; if (iweekday>7 ){ iweekday=1; document.write("</tr>"); } thisdate.setDate(nextday); } </script> </body> </html>
Related recommendations: JavaScript learning tutorial.
The above is the detailed content to help you get JavaScript objects. For more information, please pay attention to other related articles on the PHP Chinese website!