JavaScript is almost a language that Web developers must learn today, but a lot of people just stop at the basics of form validation, and in the presence of object-oriented languages, we need to learn the object-oriented knowledge of JavaScript, To better master JavaScript and lay the groundwork for a deeper understanding of the various scripting frameworks.
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.
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.
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
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.
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.
function Person(username, age) {
this.Name = username;
this.Age = age;
this.Introduce = function() {
alert("我叫" + this.Name + ",今年" + this.Age + "岁了。");
};
};
var person1 = new Person("张三", 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.
function Person(username, age) {
this.Name = username;
this.Age = age;
this.Introduce = function() {
alert("我叫" + this.Name + ",今年" + this.Age + "岁了。");
};
};
var PersonClass = Person;
var person1 = new PersonClass("张三", 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.