JavaScript Learning notes 5 Classes and objects _ basics

Source: Internet
Author: User
Three characteristics of object-oriented language: inheritance, polymorphism, encapsulation, these three points although JavaScript does not provide a natural syntax implementation, but we can all through the prototype and other techniques to achieve, so this is not too much to say.
In JavaScript, there are three ways to construct objects:
1. First, we want to be clear about a concept that JavaScript is a weak type of language, on the one hand, in JavaScript variables, return types are not strongly typed constraints, on the other hand, JavaScript can add properties and methods to the object arbitrarily. Based on this, we can write code like this:
Copy Code code as follows:

<script type= "Text/javascript" >
var person = {};
Person.name = "Fei Lin sha";
Person.age = 21;
Person. introduce = function () {
Alert ("My name is" + THIS.name +). I ' m ' + this.age);
};
Person. Introduce ();
</script>

The person here is one of the objects we construct.
2. We can also use the form of JSON to construct an object.
Copy Code code as follows:

<script type= "Text/javascript" >
var person = {
Name: "Fei Lin Sha",
Age:21,
Introduce:function () {alert ("My name is" + THIS.name +). I ' m "+ this.age);}
};
Person. Introduce ();
</script>

Does this look like the anonymous object we raised in c#3.0?
Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
var person = new
{
Name = "Flying Forest",
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 it can be assigned to a variable, but C # cannot.
But the above two methods we see, we are all individually defined an object. Next let's abstract them out into a class.
Copy Code code as follows:

<script type= "Text/javascript" >
var person = function () {
THIS.name = "Fei Lin sha";
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 see that attributes have been written to death, we simply cannot customize each object individually, the solution is simple:
Copy Code code 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 Lin Sha", 21);
Person. Introduce ();
</script>

Well, let's compare the second and third ways, and the two are equivalent. In the second way, we actually build a JSON object, and we know that JSON is essentially a key-value pair, so can we understand an object in the same way?
Let's try to write this test code:
Copy Code code 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 Lin Sha", 21);
For (var p in person) {
Alert (p);
}
Alert (person["name"]);
</script>

This code does not have any problems, first of all by traversing the way to find all the key (attribute and method name). We then use the indexed method to access the Name property of the Person object.
These are all right, but do we see an extension of the problem, from the traditional object-oriented language, name and age should belong to the private variable, so the simple access to person, is not the destruction of encapsulation?
Do you remember what we said in the previous article? var is called variable and has no called attribute of var. So if we change the code to look like this.
Copy Code code 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);
};
};
The var person = the new person ("Fly E Lin Sha 3", 21);
Alert (person["name"]);
Alert (person. GetName ());
</script>

This can be encapsulated very well, and this is also a way of encapsulating in JavaScript.
OK, here are the classes and objects about JavaScript, but there are still some problems. We will mention this in the following paragraphs.
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.