First of all, I have to say that I cannot go beyond the concepts of classes and objects to see the Javascript realm. There are many statements about whether Javascript is object-oriented, but what I most agree with is that JavaScript is a prototype-based object-oriented language ".
Three main features of object-oriented language: inheritance, polymorphism, and encapsulation. Although JavaScript does not provide a natural syntax implementation, we can use prototype and other techniques to implement it, therefore, this statement does not seem to be sufficient.
In JavaScript, there are three methods to construct an object:
1. first, we need to clarify the concept that JavaScript is a weak type language. On the one hand, it is reflected in Javascript variables, and the return type has no strong type constraints. On the other hand, javascript can add any attributes and methods to an object. Based on this, we can write suchCode:
<ScriptType= "Text/JavaScript"> VaRPerson = {}; person. Name ="Feilinsa"; Person. Age = 21; person. Introduce =Function() {Alert ("My name is"+This. Name +". I'm"+This. Age) ;}; person. Introduce ();</Script>
The person here is an object we have constructed.
2. We can also construct an object in JSON format.
<ScriptType= "Text/JavaScript"> VaRPerson = {Name:"Feilinsa", Age: 21, introduce:Function() {Alert ("My name is"+This. Name +". I'm"+This. Age) ;}}; person. Introduce ();</Script>
Is this like the anonymous object we proposed in C #3.0?
Protected voidPage_load (ObjectSender,EventargsE ){VaRPerson =New{Name ="Feilinsa", Age =21}; Response. Write ("My name is"+ Person. Name +". I'm"+ Person. Age );}
The difference is that in Javascript, a function is a type, so a variable can be assigned, but C # cannot.
However, the above two methods show that we define an object separately. Next let's abstract them into a class.
< Script Type = "Text/JavaScript"> VaR Person =Function (){ This . Name = "Feilinsa" ; This . Age = 21; This . Introduce = Function () {Alert ( "My name is" + This . Name + ". I'm" + This . Age );};}; VaR Person = New Person (); person. Introduce (); </ Script >
But here, we can see that all attributes have been written to death, and we cannot customize each object separately. The solution is simple:
< Script Type = "Text/JavaScript"> VaR Person = Function (Name, age ){ This . Name = Name; This . Age = age; This . Introduce = Function () {Alert ( "My name is" + This . Name + ". I'm" + This . Age );};}; VaR Person = New Person ( "Feilinsa" , 21); person. Introduce (); </ Script >
Well, let's compare the second and third methods. The two are equivalent. In the second write method, a JSON object is actually built, and we know that JSON is actually a key-value pair, can we understand an object in the same way?
Let's write this test code and try:
< Script Type = "Text/JavaScript"> VaR Person = Function (Name, age ){ This . Name = Name; This . Age = age; This . Introduce = Function () {Alert ( "My name is" + Name + ". I'm" + Age );};}; VaR Person =New Person ( "Feilinsa" , 21 ); For ( VaR P In Person) {alert (p);} alert (person [ "Name" ]); </ Script >
This code has no problem. First, we use the Traversal method to find all the keys (attributes and method names) of the person ). Then we use the index method to access the name attribute of the person object.
There is no problem with this, but we have not seen an extended problem. From the perspective of traditional object-oriented languages, name and age should belong to private variables, so we can use person for simple access, does it damage encapsulation?
Do you remember what we mentioned earlier? VaR is called a variable, and VaR is not called an attribute. So we can change the code to this.
< Script Type = "Text/JavaScript"> VaR Person = Function (Name, age ){ VaR Name = Name; VaR Age = age; This . Getname = Function (){ Return Name ;} This . Getage = Function (){ Return Age ;} This . Introduce = Function () {Alert ( "My name is" + Name + ". I'm" + Age );};}; VaR Person = New Person ( "Feélin? Sha 3" , 21); alert (person [ "Name" ]); Alert (person. getname ()); </ Script >
This can be encapsulated well, which is also the encapsulation method in JavaScript.
Well, this is all about JavaScript classes and objects, but there are still some problems. We will mention it later.