Use other advanced languages, such as VB. NET, C #. NET, VC + 6.0, very fond of the class, and in the early stages of learning Javascript, it was also entangled because there is no class.
After a period of exploration, I discovered how to use Javascript classes. I would like to share with you here.
Let's look at the differences and relationships between JavaScript classes and VB. NET classes.
The following is an example of a VB class used to display information boxes:
Public Class Message Public msgInfo As String = "" Public Sub ShowMessage() MsgBox(msgInfo) End Sub Public Sub ShowMessage(ByVal newMessage As String) msgInfo = newMessage MsgBox(msgInfo) End SubEnd Class
Similarly, a JavaScript class is used to display the information box:
Function Message () {this. msgInfo = ""; this. ShowMessage = function (newMessage) {if (! NewMessage) {// if no alert (this. msgInfo);} else {// If msgInfo = newMessage; alert (msgInfo) ;}} function onBtnMessageClick () {var oMessage = new Message (); oMessage. msgInfo = "one way"; oMessage. showMessage (); oMessage. showMessage ("hi, way two ");}
After comparison, we can get the following points:
1. In JS, a function can be new into an object, so this function can also be called a "function class"
2. For member variables in JS, you need to use "this." For the price. When referenced by the member function, you also need to use "this." + variable name for reference.
3. In JS, all parameters are optional. You can use this method to simulate function overloading.
When calling a function, if no parameter is passed, the value of the variable corresponding to the parameter list here is undefined.