Javascript also has some object-oriented features, like java, C# and other languages, but when you compare it carefully, you will find that these features are not really object-oriented. Many places use the object itself to simulate object-oriented, so it is believed that javascript cannot be considered an object-oriented programming language, but an object-based language.
In JavaScript, everything is really an object. The things that come out of new are objects, methods are objects, and even classes are objects. Let’s take a look at the object characteristics of objects, methods and classes.
1. Take the built-in Date and take a look
var time = new Date();
var timeString = () + "-" +
() + "-" +
() + " " +
() + ":" +
() + ":" +
();
(timeString);
By operating the Date object it refers to, you can easily call a series of getXX() methods contained in the Date object to obtain information such as year, month, day, time, minute, and second.
You can take a look at String
var username = new String("hello world");
();
The variable username refers to the new string object and accesses the length property of the string object through username.
2. Methods are also objects
function hello() {
alert("hello");
};
var helloRef = hello;
helloRef();
hello is a method, helloRef is a variable that references hello methods. helloRef and hello point to the same method object. This means that helloRef can also be executed, helloRef(). Similarly, you can also write the following code.
var helloRef = function() {
alert("hello");
};
helloRef();
function(){alert("hello")} is an anonymous method, and of course it is also an object. After referring to the method object with the helloRef variable, the method can be called through helloRef.
3. What about the class?Of course, classes are also objects. In JavaScript, there is not a class keyword like C# or java to create classes, but directly use the keywords of the method to create classes or mock classes.
function Person(username, age) {
= username;
= age;
= function() {
alert("My name is" + + ", this year" + + "year-old.");
};
};
var person1 = new Person("Zhang San", 20);
();
The above creates a Person type. Person has constructed parameters username and age. The created Person object can call the method Introduction contained in Person. The following is some modifications to the code.
function Person(username, age) {
= username;
= age;
= function() {
alert("My name is" + + ", this year" + + "year-old.");
};
};
var PersonClass = Person;
var person1 = new PersonClass("Zhang San", 20);
();
Redeclare the new variable PersonClass and reference the Person class. PersonClass and Person both 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 get a glimpse of everything in JavaScript.
The next section will talk about objects in javascript in detail.
In JavaScript, everything is really an object. The things that come out of new are objects, methods are objects, and even classes are objects. Let’s take a look at the object characteristics of objects, methods and classes.
1. Take the built-in Date and take a look
Copy the codeThe code is as follows:
var time = new Date();
var timeString = () + "-" +
() + "-" +
() + " " +
() + ":" +
() + ":" +
();
(timeString);
By operating the Date object it refers to, you can easily call a series of getXX() methods contained in the Date object to obtain information such as year, month, day, time, minute, and second.
You can take a look at String
Copy the codeThe code is as follows:
var username = new String("hello world");
();
The variable username refers to the new string object and accesses the length property of the string object through username.
2. Methods are also objects
Copy the codeThe code is as follows:
function hello() {
alert("hello");
};
var helloRef = hello;
helloRef();
hello is a method, helloRef is a variable that references hello methods. helloRef and hello point to the same method object. This means that helloRef can also be executed, helloRef(). Similarly, you can also write the following code.
Copy the codeThe code is as follows:
var helloRef = function() {
alert("hello");
};
helloRef();
function(){alert("hello")} is an anonymous method, and of course it is also an object. After referring to the method object with the helloRef variable, the method can be called through helloRef.
3. What about the class?Of course, classes are also objects. In JavaScript, there is not a class keyword like C# or java to create classes, but directly use the keywords of the method to create classes or mock classes.
Copy the codeThe code is as follows:
function Person(username, age) {
= username;
= age;
= function() {
alert("My name is" + + ", this year" + + "year-old.");
};
};
var person1 = new Person("Zhang San", 20);
();
The above creates a Person type. Person has constructed parameters username and age. The created Person object can call the method Introduction contained in Person. The following is some modifications to the code.
Copy the codeThe code is as follows:
function Person(username, age) {
= username;
= age;
= function() {
alert("My name is" + + ", this year" + + "year-old.");
};
};
var PersonClass = Person;
var person1 = new PersonClass("Zhang San", 20);
();
Redeclare the new variable PersonClass and reference the Person class. PersonClass and Person both 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 get a glimpse of everything in JavaScript.
The next section will talk about objects in javascript in detail.