Basic of javascript object-oriented technology (III)

Source: Internet
Author: User

I have read many introductionsJavascriptThe article on object-oriented technology is dizzy. Why? It's not because it's not well written, but because it's too esoteric. The objects in javascript haven't clearly explained what's going on. As soon as they come up, they go straight to the topic, class/Inheritance/prototype/private variable. As a result, after reading it for a long time, I had a rough understanding and had a good aftertaste. It seems that I did not understand anything.

This article is written in chapter 7, 8 and 9 of <javascript-the definitive guide, 5th edition>, I will try to describe the javascript object-oriented technology (Object/array-> function --> class/constructor/prototype) according to the structure of the original book ). I will attach the original English statement for your reference.

If no description is provided, all the English statements (except the program body) in the text are derived from <javascript-the definitive guide, 5th edition>.

Function
Javascript functions have been written a lot, so here is a brief introduction.

Create a function:

Function f (x ){........}

Var f = function (x ){......}

You can create a function named f () in the preceding two forms. However, you can create an anonymous function in the latter form. You can set parameters when defining a function. If the number of parameters passed to the function is insufficient, the parameters are matched from the leftmost, and the rest are assigned with undefined values. If the number of parameters passed to the function is greater than the number of function-defined parameters, the extra parameters are ignored.

Js Code

 
 
  1. function myprint(s1,s2,s3) {  
  2. alert(s1+"_"+s2+"_"+s3);  
  3. }  
  4. myprint(); //undefined_undefined_undefined  
  5. myprint("string1","string2"); //string1_string2_undefined  
  6. myprint("string1","string2","string3","string4"); //string1_string2_string3  

Therefore, for a defined function, we cannot expect the caller to pass all the parameters in. The required parameters should be checked in the function body (use! Operator), or set the default value, and then perform the OR (|) operation with the same parameter to obtain the parameter.

Js Code

 
 
  1. Function myprint (s1, person ){
  2. Var defaultperson = {// default person object
  3. "Name": "name1 ",
  4. "Age": 18,
  5. "Sex": "female"
  6. };
  7. If (! S1) {// s1 cannot be blank
  8. Alert ("s1 must be input! ");
  9. Return false;
  10. }
  11. Person = person | defaultperson; // accepts the parameter of the person object.
  12. Alert (s1 + "_" + person. name + ":" + person. age + ":" + person. sex );
  13. };
  14. Myprint (); // s1 must be input!
  15. Myprint ("s1"); // s1_name1: 18: female
  16. Myprint ("s1", {"name": "sdcyst", "age": 23, "sex": "male"}); // s1_sdcyst: 23: male

Arguments attribute of the Function

Each function body contains an arguments identifier, which represents an Arguments object. the Arguments object is very similar to an Array object. For example, all objects have the length attribute. to access its value, use the "[]" operator to access the parameter value through indexes. However, the two are completely different. They only have something in common on the surface (for example, modifying the length attribute of the Arguments object does not change its length ).

Js Code

 
 
  1. function myargs() {  
  2. alert(arguments.length);  
  3. alert(arguments[0]);  
  4. }  
  5. myargs(); //0 --- undefined  
  6. myargs("1",[1,2]); //2 --- 1  

The Arguments object has a callee attribute that identifies the method of the current Arguments object. You can use it to implement internal recursive calls to anonymous functions.

Js Code

 
 
  1. function(x) {  
  2. if (x <= 1) return 1;  
  3. return x * arguments.callee(x-1);  
  4. }  

Method -- Method

The method is a function. we know that each object contains 0 or more attributes. attributes can be of any type, including objects. A function itself is an object, so we can put a function into an object. At this time, this function becomes a method of the object. if you want to use this method later, you can use ". "operator to implement.

Js Code

 
 
  1. Var obj = {f0: function () {alert ("f0") ;}}; // The object contains a method
  2. Function f1 () {alert ("f1 ");}
  3. Obj. f1 = f1; // Method for adding an object
  4. Obj. f0 (); // f0 f0 is the obj method.
  5. Obj. f1 (); // f1 f1 is the obj method.
  6. F1 (); // f1 f1 is a function and can be called directly.
  7. F0 (); // f0 is only the obj method and can only be called through objects.

Method calls require the support of objects. How can we obtain object attributes in methods? This! We are already familiar with this keyword. In javascript methods, we can use this to obtain reference to method callers (objects) and obtain various attributes of method callers.

Js Code 

 
 
  1. Var obj = {"name": "NAME", "sex": "female "};
  2. Obj. print = function () {// Add Method for Object
  3. Alert (this. name + "_" + this ["sex"]);
  4. };
  5. Obj. print (); // NAME_female
  6. Obj. sex = "male ";
  7. Obj. print (); // NAME_male

Here is a more object-oriented example.

Js Code

 
 
  1. var person = {name:"defaultname",  
  2. setName:function(s){  
  3. this.name = s;  
  4. },  
  5. "printName":function(){  
  6. alert(this.name);  
  7. }}  
  8. person.printName(); //defaultname  
  9. person.setName("newName");  
  10. person.printName(); //newName  

In the above example, you can use person. name = .. to directly change the name attribute of person. Here we just want to show the content just mentioned. another way to change the person attribute is to define a function and receive two parameters, one is person and the other is name, which looks like this: changeName (person, "newName "). which method is better? Obviously, the methods in this example are more visual and intuitive, and seem to have an object-oriented shadow.

Once again, the Method itself is a function, but the use of the Method is more restricted. In the subsequent sections, If we mention the function
The content mentioned also applies to methods, but not all.

Prototype attribute of the Function

Each function contains a prototype attribute, which forms the core foundation of javascript object-oriented. We will discuss it in detail later.

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.