JavaScript Object-Oriented Programming (3): Object

Source: Internet
Author: User
1. ObjectObjects are one of the basic concepts of object-oriented programming. You only need to look at this name and you will know it. In our familiar object-oriented languages, such as Java or C ++, there are similar object definition methods. For example, we want to define a class named Person, which has two attributes: name and age. Another method will display the name and age of the Person object, we can use the following code to implement: Java: public class Person {
Private String name;
Private int age;

Public String getName (){
Return name;
}

Public void setName (String name ){
This. name = name;
}

Public int getAge (){
Return age;
}

Public void setAge (int age ){
This. age = age;
}

Public void introduction (){
System. out. println ("My name is" + this. name + ", my age is" + this. age );
}

Public static void main (String [] args ){
Person p = new Person ();
P. setName ("Tom ");
P. setAge (20 );
P. introduction ();
}
} The implementation of c ++ is similar. I will not go into details here. Let's first take a look at the definition of this class: first declare the attribute, then define the getter and setter methods of the attribute for external access to private variables, and finally define its own method. This is a common definition method, so that many languages, such as C #, will adopt this definition. So what is an object? An object is a set of attributes and methods. Although this is not a strict definition, we put attributes and their names together (we may think of its methods as its attributes, which is no different, form a set, which is an object. That is to say, simply put, the object is in this form of a "key-value" pair.2. JavaScript objectsForm of "key-value" pair ...... Does this look familiar? Bingo! Right! Is this the form of an array? Well, congratulations! Indeed, in JavaScript, objects are defined as arrays. Next, we will define this Person in JavaScript: var Person = {
"Name": "Tom ",
"Age": 20,
"Introduction": function (){
Alert ("My name is" + this. Name + ", my age is" + this. Age );
}
};
Person. Introduction (); let's take a look at this code. It looks like the definition of an array, but the array generally uses the numeric type as the subscript, and here we use a string. Recall that in Javascript, strings can also be used as the underlying object of arrays, isn't it? Well, here we declare an object "person" with a name and an age, and a method to display these two attributes. In JavaScript, the object is in the form of a "key-value" pair, specifically "string-as-key": Object-as-value. That is to say, the key must be of the string type, and the value can be of any type. What about the method? In fact, the function in Javascript is also a type, which will be described later. Here, we can only know it first. It is very common to become a binary group in mathematics. arrays are like this, except that the keys of arrays must be Int. Similarly, JavaScript objects are also a special Binary Group, except that keys are of the string type. Is this like a hash? Or a hash table? That's it! If you think every attribute name must be enclosed with a pair of quotation marks, you can leave it alone! Like the following statement, JavaScript fully considers your correctness: var person = {
Name: "Tom ",
Age: 20,
Introduction: function (){
Alert ("My name is" + this. Name + ", my age is" + this. Age );
}
}
Person. Introduction (); I'm used to this writing method and it looks similar to Java and other languages.3. Use of attributesThe use of attributes in Javascript may be special. Let's look at the four statements that try to use the name attribute of person. They both look similar, but they do: Alert (person. Name );
// Alert (person. "name ");
Alert (person ["name"]);
Alert (person [name]); except for the comments, all the other three statements can be interpreted (because Javascript is an interpreted language, not a compiled language, therefore, this is not a compilation statement. However, only one or three sentences can extract the name attribute! There is no difference between the first sentence and Java, the second two seem special, and the third sentence looks like what? Right! Access to array elements! This further verifies that the array and object in JavaScript are "the same root ". What about the fourth sentence? Of course, undefined is returned! Because the array subscript must be a number or string! If you are familiar with Java, you may have some questions about the introduction function. Java programmers will not add this all the time. This will be added only when a whim occurs. However, if you want to be lazy in JavaScript and remove this, the result will only be an error. What's going on? In short, the this keyword here is essential! This is a difference between JavaScript and other languages. The specific reason will be explained in a later article. Now I just want to know it.-Don't be lazy here ~~4. More attribute operationsThe current understanding of JavaScript Object Attributes should be like this: the JavaScript Object is a binary group, or a hash table. If you can understand this, you will not be surprised with the following operations: var person = {}; // create an empty object
Person. Name = "Tom"; // Add an attribute name and assign it to Tom.
Person ["Age"] = 20; // use another method to add a property
Person. Introduction = function (){
Alert ("My name is" + this. Name + ", my age is" + this. Age );
};
Person. Introduction ();
For (VAR field in person) {// use the foreach loop to list all attributes of an object
Alert ("field name:" + field + "; Value:" + person [field]);
}
Delete person. Name; // delete the name attribute
Person. Introduction ();
Alert (name in person); // use the in operator to determine whether an attribute exists.5. constructor attribute of the objectIn JavaScript, each object has a constructor attribute. This constructor attribute is used to record the name of the constructor during object initialization. For example, VAR date = new date ();
Alert (date. constructor );
Alert (date. constructor = "date"); // false
Alert (date. constructor = Date); // true well, this Date is a built-in JavaScript Object. The code here looks very common. However, if you want to use a self-written Object, such as the previous Person, you will find that its constructor Object is an Object? There are two problems: first, we didn't give the Person constructor attribute. How can it be there? Second, how is the constructor attribute object, not our Person? For the first question, it is obvious that JavaScript has added it to us. In fact, every JavaScript Object has such an attribute. So how is its value an Object? This problem will be explained when we talk about the new operator. Note that the objects in this Article refer to the objects obtained after new is used separately. That is to say, the constructor attribute is obtained during the new operator. This involves constructors. However, this is not the focus of this Article. Let's talk about 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.