[JavaScript learning records] classes in JavaScript

Source: Internet
Author: User

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.
 

 

 

 

 

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.