Javascript Study Notes [6]-object-oriented

Source: Internet
Author: User
Tags prototype definition

Today is another day at home. I read object-oriented programming, including the following chapters, creating and using classes, class member access control, inheritance and combination, static members and static classes, and polymorphism, prototype chain. Because you have already learned other object-oriented languages, it is faster. It is expected to enter the Javascript built-in class tomorrow.

There was a slightly sad thing. The study notes I wrote yesterday [5] -- function 2 was removed from the homepage by the blog garden. It is estimated that the Administrator sent three essays a day, and the depth is not enough, so I can't see it, but it makes me sad, sad, good, this section is as long as possible to write a little longer, and then more understanding, simple knowledge is not enough. Ah, this learning thing is actually like this ..

6.1 object-oriented-Basic Concepts

I will not elaborate on it here, but I will list some basic knowledge points:

Object, instance, and class (instance of the class when the object is located)

Advantages: reusable and easy to maintain

Basic Features: encapsulation, inheritance, polymorphism (Class One method and multiple forms), (Abstract)

6.2 create a class -- constructor Method

Use a function to create a class. The function itself is also called the constructor of the class (also called the constructor method and constructor method ).

Javascript is prototype-Based Object-Oriented ----Important.

View code

  function   person () {  This  . name;   This  . age;   This . showinfo =  function   () {
     return  "my name is" +  This . name + "me" +  This . age + "years old. ";}  var  Tom =  New   person (); Tom. name  = "Tom" ; Tom. age  = 22 ;  var  info =  Tom. showinfo (); document. write (info  + "
");

New, object, and array can be instantiated without the new keyword.

6.3 define methods and attributes

1) This keyword--- View the aboveCodeDefine attributes through this (in fact, it is easy to make mistakes when using this keyword, especially the closure function. If you remember this, I will summarize it)

2) prototype Definition

View code

 //  The prototype definition method and attributes are similar.  Function  Person (myname, myage ){  This . Name = Myname; This . Age = Myage;} person. protoype. showinfo = Function  (){  Return "Name =" + name + ", age =" + Age ;};  //  Another important role of prototype is to add new members to the class.   Function  Showdetail (){  Return "I am a cainiao" ;} Person. Prototype. showdetail = Showdetail; VaR Person = New Person ("Tony", 22 ); Document. Write (person. showdetail ()); 

 

3) object class, return Extension

The object class directly defines an instance and assigns attributes and methods to the object, which reflects the concept of JavaScript name-value pairs. See the following code.

View code

VaRPerson ={Name:"Tony", Age:22, Showinfo:Function(){Return"Name =" + name + ", age =" +Age ;}} document. Write (person. showinfo ());

 

The following describes how to use the return statement to define methods and attributes, and how to use prototype and object to define methods and attributes.

View code

 Function  Person (myname, myage ){  Return  {Name: myname, age: myage, showinfo: Function  (){  Return "Name =" + name + ", age =" + Age ;}}}  //  Prototype and object work together to define methods and attributes for Classes  Function  Person () {} person. Prototype = {Name: "Tony" , Age: 22 , Showinfo:  Function  (){  Return "Name =" + name + ", age =" + Age ;}}; 

 

6.4 Use get and set accessors --- get in Java, the set method is the same

6.5 class and member access control --- public and private variables

6.6 inheritance and combination

Inheritance: Remember child. Prototype = new father (), which implements inheritance. You can use the constructors of the base class to implement the super method in other languages.

Combination: Remember the hands, legs, and other parts of the first class. This is the combination idea. The specific code will not be pasted, that is, in the combination class (person) create a new hand in the implementation method, and then use the hand method.

Here we have a concept: Is-A and has-. Is-A is a concept, and has-a is a concept.

6.7 static members, static classes, and enumeration

Static class: it is compared with those unique in reality. There is a moon in the world, so it can be defined as a static class. To declare a member as static, you only need to assign the attribute or method to the class itself. That's it. There are still some precautions.

6.8 polymorphism-heavy load and coverage

A class has multiple methods with the same name-overload, but JavaScript does not support defining multiple methods for overloading. The following code uses arguments. length determines the number of parameters, and typeof determines the parameter type to achieve overload

View code

 Function  Overloadtest (){  VaR Len = Arguments. length;  If (LEN = 2 ){ If ( Typeof (Arguments [0]) = "Number "&& Typeof (Arguments [1]) = "Number" ){  //  Execute a function } Else   If ( Typeof (Arguments [0]) = "string "&& Typeof (Arguments [1]) = "string" ){}  Else  { Throw   New Error ("two parameter types are incorrect" );}}  If (LEN = 3 ){  If  (){}  Else   If  (){}  Else  {}}} 

 

Override the method of the base class in the new class-override. You only need to define a new method member with the same name for the subclass to implement the override. No code is written.

6.9 prototype chain essence-Key Points

I also don't know a lot of things here, so I want to write more. Let's study it together. Knowledge points are as follows:

Each class has a prototype attribute (static attribute), which indicates an object of the class, that is, a prototype object.

The properties defined on the prototype object contain information about the base class, and prototype on the base class also has its base class information, in this way, a chain-prototype chain-caused by the inheritance relationship

The end of the prototype chain is the prototype object of the object. The internal prototype value of the object is null.

ECMA-262 rules: when the class definition, it has the original type object, package some internal specific attributes, as the characteristics of the class, the two internal attributes are prototype and class name ). You can use _ PROTO _ to access a non-IE browser, for example:

Array. Prototype. _ PROTO _ = object. prototype.

Components of the prototype chain: Implement inheritance and share attributes (you can see the above Code)

The following are several methods:

The object. getprototypeof () method waits until the prototype attribute of the specified object replaces the _ PROTO _ attribute.

Class. Prototype. isprototype (object) to check whether the object is in the prototype chain of the specified object.

Oobject. hasownproperty (propname) to check whether the specified object has defined a specific property

Object. propertyisenumerabel () to check whether the specified attribute exists and whether it can be enumerated.

That's all. It's quite late. Continue tomorrow.

 All of the above are personal originals. Please refer to the original link when you reprint it:Http://www.cnblogs.com/tonylp

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.