Javascript basics and object-oriented Basics ~ Fifth, the methods in JavaScript and the methods in the class

Source: Internet
Author: User
ArticleDirectory
    • The simplest function (method ):
    • Form Verification Function
    • Methods (functions) in the object)
    • Sub-methods in the Method
    • Method Return Value
    • Methods can be passed as parameters (delegate in JS, callback function)

Back to directory

The previous article mainly talked about classes in JS. Many of my friends have replied to my article. I am very grateful to you for your reply to some of your questions, "class" is just an idea. We should not focus on "whether a language provides classes or whether classes are implemented". This is not important, what is important is the idea of classes in object-oriented systems!

Today, we mainly talk about methods. Methods in JS are also called function functions.CodeIt is similar to object-oriented encapsulation. You don't need to know the implementation details of a method. You just need to call it and understand the signature of the method.

The simplest function (method ):
 <  Script  Type  = "Text/JavaScript"  >          // Addition operation          Function  Sum (x, y ){  Return  Parseint (X)  +  Parseint (y );}  //  Subtraction          Function  Sub (x, y ){ Return  Parseint (X)  -  Parseint (y );}  </  Script  > 
Form Verification Function

The following is a form verification function. If the verification is successful, the form is submitted. If the verification is not successful, an error message is returned.

 <  Script  >            //  Form Verification. You can use this function in multiple forms to complete verification.         Function  Checkform (){  VaR  Name  =  Document. getelementbyid (  "  Name  "  );  VaR Password  =  Document. getelementbyid (  "  Password  "  );  VaR  Email  =  Document. getelementbyid ( "  Email  "  );  If  (Name  =   ""  ) {Alert (  "  Enter User Name  " );  Return   False  ;}  If  (Password  =   ""  ) {Alert (  "  Enter Password "  );  Return   False  ;}  If  (Email  =   ""  ) {Alert (  " Enter the email address  "  );  Return   False  ;} Document. getelementsbytagname (  "  Form  "  )[  0 ]. Submit ();  Return   True  ;}  </  Script  > 
Methods (functions) in the object)

In fact, the function in JS is like this, and the function can also appear in the object, similar code also appears in the previous file, and then write:

//Item objectVaRProps ={Init:Function(){//Methods In the object//Object initialization}, Gethtmltags:Function(){//Construct an HTML code block}, Language :["Chinese", "English"], Name:"Item"};
Sub-methods in the Method

In addition, there can be sub-methods in the method. In the Sub-method, you can override the attributes in the parent method to change its value. The Code is as follows:

FunctionBig (){VaRName = "method big";FunctionSmall () {name= "Method small";} Small ();ReturnName;} alert (big ());
Method Return Value

The Return Value of the method can be null, Boolean, integer, string, and an object. The Code is as follows:

FunctionReturnobject (){VaRName = "zzl";Return{//Returns an object whose attributes are publicly available.Name: name, age:30};} Alert (returnobject (). Name );
Methods can be passed as parameters (delegate in JS, callback function)

A method can be used as the receiving parameter of a method. This is generally called a callback function and is called a delegate in C.

//Passing functions as parametersFunctionAlertbox (MSG, callback ){If(MSG! =Null) {Callback (MSG );}Else{Alert ("Null message");}}//JS callback functionAlertbox ("cute ",Function(MSG) {alert (MSG );});

Today, I am talking about functions (methods). I am also happy to write them myself. I have no head or tail and write them there. But I have basically said everything about function usage!

Thank you for reading this article!

Back to directory

 

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.