JavaScript Object-oriented learning--1

Source: Internet
Author: User

Company projects using EXT, the results I found the entry is not easy! In particular, using JavaScript to write object-oriented programs, often using jquery know that jquery is function-oriented programming, it is easy to get started. However, ext is object-oriented, so when you want to customize the Ext component, or use the Ext component, it can be very upsetting. Therefore, you should first learn JavaScript object-oriented basis, and then look at the ext source code.

This will be the post-learning route to the blog route.

1 JavaScript is an object-oriented language based on the prototype (Prototype based) , the Java language, which is based on the class-mode (classes based). Well, we can't inherit the parent class's properties and methods by inheriting a subclass like Java. In any case, instantiating an object can be done in new way.

2 The definition of a JavaScript class is the definition of a function, the question is, what is the difference between a class and a function? Do I call a function to instantiate an object? So I'm going to take notes here and make a distinction between the two points!

Function father (name , age) {   var sex ; //  private properties, local variables    this.name = name; //  member properties   this.age = age; //  member properties   sex =  ' man ';     this.setsex = function (value) {     sex = value;  //  Assigning private Properties   };    this.getsex  = function () {     return sex; //  returns the value of the private property, and the value of the property cannot be obtained directly from the instance outside    };    return name+ ' _ ' +age+ ' _ ' +sex; //  function return value, object instance when this does not work   }var f1 = new father (' maven ', '; //  ') instantiates an object var f2 =  father (' svn ', ';//  ') Call function Console.info (F1); //  object structure Console.info (F2);//  function return value Console.info (f1.sex); //  cannot get private property Console.info (F1.name);//  get member Properties Console.info (F1.getsex ()) ;//  Get Member Method Console.info (F2.sex);/ f2 is just a string return value, so the following call is the wrong Console.info (f2.name); Console.info (F2.value); 

Summarize:

1) Call the function directly, essentially returning the function's return value, the method defined in the function, the property can not be accessed outside. Because a function is a scope block, the code block runs in memory when the function is called, the function ends, and local variables inside the scope block are recycled. Therefore, you cannot access the properties and methods defined inside.

2) Adopt new mode, because JavaScript does not have a class definition, we just treat the function definition as the definition of the class. With new, the system opens up a space in the memory stack to store the object's properties and methods, while returning the object's references. Therefore, the properties and methods registered when the class is defined are not reclaimed by the garbage collection mechanism, so the object can invoke the previously registered properties and methods.

3 The point of this

Call the above code console.info (name); Output Svnconsole.info (this.name);//Output Svnconsole.info (f1.name);//Output Mavenconsole.info (f2.name);//Output maven// Call the above code Console.info (Getsex ());//Output ' man ' Console.info (This.getsex ());//Output ' man ' Console.info (F1.getsex ()); Output ' man ' Console.info (F2.getsex ()); Error

Summarize:

1) This is used in the function scope block to define the scope of the attribute, however this is specific to who needs to see how to use it!

2) If the definition of a function is run by means of a new object, the this in this function points to the current object;

3) If the function is called by itself, then this is a pointer to the object that called the function, where the function is called by window because we declared the global object, so this is a point to the window

4) All in all: Except for new, This is all directed to the caller, " who called me, who isthis", and note that:

Father.son.sing ();

If there is this in the Sing method, then this is pointing to son, because it is the son that called Sing not father.

Therefore, we generally do not use this directly in project development, but instead use the var me = this; This calls me every time, without confusing the point of this.

3 about Var defining variables as private members

I believe that people who have studied JavaScript know that if you use Var to define a global variable, you will essentially add a property to the Window object. But why do we not add a property to an instance in a function that defines a variable of type var?


JavaScript Object-oriented learning--1

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.