Javascript learning notes (8) javascript objects

Source: Internet
Author: User

1. built-in objects in javascript
In addition to built-in objects in javascript, we are familiar with the following objects:
• Array
• Date
• Math
• String
• RegExp
•......
Each object has its own attributes and methods. For example, we often use attributes and methods.
Attribute: stringObject. length; arrayObject. length ;......
Method: stringObject. indexOf (); stringObject. splite (); stringObject. substr (); arrayObject. concat (); arrayObject. push (); arrayObject. join ();......
2. How to customize objects and add attributes and Methods
A. Create with the keyword "new"
Copy codeThe Code is as follows:
Var newObject = new Object (); // create a new class
NewObject. name = "new object"; // Add a name attribute
NewObject. say = function () {// Add the say () method
Alert (this. name); // output new object
}

For the above creation method, we can use the JSON (JavaScript Object Notation) method to abbreviated as the following code:
Copy codeThe Code is as follows:
Var newObject = {
Name: "new object ";
Say: function (){
Alert (this. name );
}
};

We use the JSON data format to create a more complex object.
Copy codeThe Code is as follows:
Var company = {
Name: "tuanzz ",
Product: "groupon ",
Address: {province: "Hubei", city: "wuhan "},
Person :[
{Name: "zhangchen", age: "23 "},
{Name: "luomi", age: "23 "},
],
Readme: function (){
Alert ("My name is" + this. person [0]. name + "and" + this. person [0]. age + "years old ");
}
};
Company. readme (); // output My name is zhangchen and 23 years old;

We can see that the Code not only looks very elegant for the objects created in JSON data format.
The JSON format is the list of projects included with the large "{}". Each project is separated by a comma (,), and the project is a colon (:):. This is a typical dictionary representation, and again shows that the objects in JavaScript are dictionary structures. No matter how complex an object is, it can be created and assigned a value by a JSON code.
B. Create objects through Constructors
Copy codeThe Code is as follows:
Function objectFun (name ){
This. name = name;
This. say = function (){
Alert (this. name );
}
}
Var newObject = new objectFun ("zhangchen ");
NewObject. say (); // output zhangchen

First, create an objectFun () function, which defines attributes and Methods. Here we can regard objectFun as a class (in javascript, a function is an object), and then create an object through the new instance, newObject also has attributes and methods in the parent class.
We can use the following code to check whether a function is an object:
Copy codeThe Code is as follows:
// Common functions
Function say (s ){
Alert (s );
}
Say ("hi ");
// Assign an attribute to a function object. A function is an object.
Say. test = "it can work? ";
Alert (say. test); // output it can work?

How to Understand the above method of creating objects? Let's look at the following code:
Copy codeThe Code is as follows:
Function objectFun (name ){
This. name = name;
This. say = function (){
Alert (this. name );
}
}
Var newObject = new Object (); // create an empty Object
ObjectFun. call (newObject, "zhangchen"); // call the objectFun function using newObject as the this parameter
NewObject. say ("zhangchen"); // output zhangchen

First, create a newObject. newObject is used as the this parameter to call the objectFun function. After talking about this, we can use objectFun as the constructor.
Certificate -------------------------------------------------------------------------------------------------------------------------------------------------
For the remaining content, refer to the understanding of JavaScript written by Li Zhan.

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.