Javascript Study Notes Class 5 and objects

Source: Internet
Author: User

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 the following code:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var person = {};
Person. name = "Fei linsha ";
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.
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var person = {
Name: "Fei linsha ",
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?
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
Var person = new
{
Name = "Fei linsha ",
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.
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var Person = function (){
This. name = "Fei linsha ";
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:
Copy codeThe Code is as follows:
<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 ("Fei linsha", 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:
Copy codeThe Code is as follows:
<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 ("Fei linsha", 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.
Copy codeThe Code is as follows:
<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 ("February? 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.

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.