Javascript object-oriented programming all objects _ js object-oriented

Source: Internet
Author: User
Javascript has almost become a required language for web developers nowadays, but many people only stop at some basic operation layers such as form verification. Currently, object-oriented languages are widely used, we need to learn the Object-Oriented Knowledge of javascript to better understand javascript and lay a solid foundation for a deep understanding of various Script frameworks. Javascript and java, C #, and other languages also have some object-oriented features. However, when comparing these features, we will find that these features are not really object-oriented, in many cases, the object itself is used to simulate object-oriented programming. Therefore, javascript cannot be regarded as an object-oriented programming language, but an object-based language.
In javascript, everything is really an object. new things are objects, methods are objects, and even classes are objects. The following describes the Object Features of objects, methods, and classes.
1. Let's take a look at the built-in Date.

The Code is as follows:


Var time = new Date ();
Var timeString = time. getFullYear () + "-" +
Time. getMonth () + "-" +
Time. getDate () + "" +
Time. getHours () + ":" +
Time. getMinutes () + ":" +
Time. getSeconds ();
Document. write (timeString );


You can use time to operate the referenced Date object. You can conveniently call a series of getXX () methods contained in the Date object to obtain information such as year, month, day, hour, minute, and second.
Let's take a look at the String.

The Code is as follows:


Var username = new String ("hello world ");
Document. write (username. length );


The variable username references the new String object and uses username to access the length attribute of the string object.
2. methods are also objects

The Code is as follows:


Function hello (){
Alert ("hello ");
};
Var helloRef = hello;
HelloRef ();


Hello is a method. helloRef is a variable that references the hello method. Both helloRef and hello point to the same method object. This means that helloRef can also be executed, helloRef (). Similarly, you can write the following code.

The Code is as follows:


Var helloRef = function (){
Alert ("hello ");
};
HelloRef ();


Function () {alert ("hello")} is an anonymous method. It is also an object. After using the helloRef variable to reference this method object, you can call the method through helloRef.
3. What about classes?Of course, classes are also objects. In javascript, unlike C # or java, class keywords are used to create classes. Instead, they are directly used to create classes or simulate classes.

The Code is as follows:


Function Person (username, age ){
This. Name = username;
This. Age = age;
This. Introduce = function (){
Alert ("My Name is" + this. Name + ", this year" + this. Age + "years old. ");
};
};
Var person1 = new Person ("James", 20 );
Person1.Introduce ();


The above creates a Person type with the construction parameters username and age. The created Person object can call the method Introduce contained in Person. Next, make some modifications to the code.

The Code is as follows:


Function Person (username, age ){
This. Name = username;
This. Age = age;
This. Introduce = function (){
Alert ("My Name is" + this. Name + ", this year" + this. Age + "years old. ");
};
};
Var PersonClass = Person;
Var person1 = new PersonClass ("James", 20 );
Person1.Introduce ();


Redeclare the new variable PersonClass and reference the Person class. Both PersonClass and Person point to the class referenced by the original Person, so you can also use PersonClass to create objects.
The above examples may not be very appropriate, but you can also have a look at all objects in javascript.
The next section details the objects in javascript.
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.