Object-oriented-Basic Concepts

Source: Internet
Author: User

What is an object?---- Objects are simple things with simple functions, such as automobiles, computers, mobile phones, and water cups. They all provide certain functions. They are a whole, integrate functions on your own.

Definition of objects in Baidu encyclopedia-class instances with attributes and methods (I think it is very abstract, but actually it is a specific thing in real life, abstract It In program design ).

Object-oriented ----Object OrientedThe concept of object-oriented is very widely used. I have seen the concept of object-oriented when learning databases, and I have also encountered the concept of VB. Of course, JavaScript has also encountered this concept. At the beginning, the object orientation will be very abstract. Simply put, the object orientation is regarded as a thing in life that provides a certain function, in JS, It is a specific encapsulation function. Such an encapsulation function can provide some functions, similar to libraries such as JQuery. It can provide many functions as front-end developers, although there is no need to know his specific code, as long as he can use the functions provided by him, but if he knows how the technical experts write the library and the thinking of writing the code, that is superior.

Object-oriented features:

  1. Abstract ----GRASP core issues. For example, in today's cet6, a person is abstracted into a name, and the admission ticket number represents a specific person.
  2. Encapsulation ---- encapsulation encapsulates data and the method (function) for processing the data into a whole to implement a highly independent module, so that users can only see the external characteristics of objects (which messages can be accepted by objects and which processing capabilities are available), and the internal characteristics of objects (private data that stores internal states and algorithms that implement processing capabilities) it is invisible to users. The purpose of encapsulation is to separate the use of the object designer from the object creator. the user does not have to know the details of the behavior implementation, but only needs to access the object with the message provided by the designer.In simple terms, it is a closed function that calls a function outside.
  3. Inheritance-single inheritance (one subclass has multiple parent classes) and multiple inheritance (one subclass has multiple parent classes)-inheritance is similar to CSS.
  4. Polymorphism ----Different actions can be taken when a message is accepted by different objects.With polymorphism, users can send a common message and leave all implementation details to the objects that receive the message. If yes, different methods can be called for the same message.

Object composition:

  • Method --- function: Dynamic
  • Variable --- property: static
1 <script> 2/* var arr = [1, 2, 3, 4]; 3 var a = 10; // The current a variable ---- free 4 arr. a = 1; // The current a is an attribute ---- 5 alert (arr. a); */6 function paxster () {7 alert ('bingo'); 8} 9 paxster (); // function 10 11 paxster = function () {12 alert ('bingostyle'); 13} 14 paxster (); // Method 15 </script>

 Who this belongs to and who it belongs

1 <script> 2 var paxster = [1, 2, 3]; 3 paxster. show = function () {4 alert (this); // this ---- who is the current method? 5} 6 paxster. show (); 7 </script>

Window object

1 function show(){2         alert(this);    3     }4     show();
  1. Function ---- function when you are free
  2. Method ---- A function is called a method when it belongs to an object.
  3. Constructor-create an object
  4. Event processing function-when processing an event

Constructor:In fact, it is easy to understand that with a function encapsulation function, other data can call this function to create new objects without having to write the same function multiple times.

1 <script> 2 // construct object 3 function createPerson (name, sex) in factory mode // constructor 4 {5 // 1. raw material 6 var obj = new Object (); 7 // 2. process 8 obj. name = name; 9 obj. sex = sex; 10 obj. showName = function () 11 {12 alert ('My name is: '+ this. name); 13}; 14 obj. showSex = function () 15 {16 alert ('I am' + this. sex + '); 17}; 18 // 3. factory 19 return obj; 20} 21 var p1 = createPerson ('paxster', 'boys'); 22 var p2 = createPerson ('boychik ', 'girl '); 23 alert (p1.showName = p2.showName); 24 </script>

 Factory problems:

  1. No new ---- new can simplify the code
  2. A waste of resources. The same function is called multiple times. (Every time a function is written, a new object is created.

Go out tomorrow morning, temporarily here :)

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.