JavaScript object-oriented Programming Everything object _js Object oriented

Source: Internet
Author: User
JavaScript and Java, C # and other languages also have some object-oriented features, but in fine comparison, it will be found that these features are not real object-oriented, many places are using the object itself to simulate object-oriented, so that JavaScript is not considered an object-oriented programming language, It is an object based language.
In JavaScript is really the object of everything, new out of things are objects, methods are objects, even classes are objects. Let's look at the object characteristics of objects, methods, and classes, respectively.
1. Take the built-in date and look at it.
Copy Code code as follows:

var time = new Date ();
var timestring = time.getfullyear () + "-" +
Time.getmonth () + "-" +
Time.getdate () + "" +
Time.gethours () + ":" +
Time.getminutes () + ":" +
Time.getseconds ();
document.write (timestring);

By using time to manipulate the date object it refers to, it is convenient to invoke a series of getxx () methods contained in the date object to obtain information such as seconds and minutes.
You can look at string again.
Copy Code code as follows:

var username = new String ("Hello World");
document.write (username.length);

The variable username references a new string object that accesses the length property of the string object by username.
2. The method is also the object
Copy Code code as follows:

function Hello () {
Alert ("Hello");
};
var helloref = Hello;
Helloref ();

Hello is a method, Helloref is a variable that references the Hello method, and Helloref and hello all point to the same method object. It means that helloref can also be executed, helloref (). Similarly, you can write the following code.
Copy Code code as follows:

var helloref = function () {
Alert ("Hello");
};
Helloref ();

function () {alert ("Hello")} is an anonymous method, and of course an object, that can be invoked by Helloref after referencing the method object with the Helloref variable.
3. What about classes? Of course, classes are objects, in JavaScript, unlike C # or Java, where class keywords are used to create classes, and instead use the keyword of the method to create a class or a mock class directly.
Copy Code code as follows:

function person (username, age) {
This. Name = Username;
This. Age = age;
This. introduce = function () {
Alert ("My name" + this.) Name + ", this year" + this. Age + "years old." ");
};
};
var person1 = new Person ("John", 20);
Person1. Introduce ();

The above creates a person type with the constructor parameters username and age, and the person object you create can invoke the method introduce that person contains. Make some changes to the code below.
Copy Code code as follows:

function person (username, age) {
This. Name = Username;
This. Age = age;
This. introduce = function () {
Alert ("My name" + this.) Name + ", this year" + this. Age + "years old." ");
};
};
var personclass = person;
var person1 = new Personclass ("John", 20);
Person1. Introduce ();

Re-declare the new variable Personclass and refer to the person class, personclass and person point to the class referenced by the original person, so you can also use Personclass to create the object.
Some of these examples may not be appropriate, but they can also be a glimpse of everything in JavaScript.
The next section is a detailed discussion of 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.