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.