JavaScript Learning Notes (eight) JavaScript Objects _ Basics

Source: Internet
Author: User
1. Built-in objects in JavaScript
JavaScript, in addition to its own built-in objects, such as the following we are familiar with the object:
Array
Date
Math
string
RegExp
•......
Each object has its own attributes and methods, such as our frequent use of attributes and methods
Properties: Stringobject.length; Arrayobject.length .....
Methods: Stringobject.indexof (); Stringobject.splite (); Stringobject.substr (); Arrayobject.concat (); Arrayobject.push (); Arrayobject.join ();
2, how to customize the object, and add properties and methods
A. Creating with keyword new
Copy Code code as follows:

var newobject = new Object (); Create a new class
Newobject.name = "New Object"; Add a property of name
Newobject.say = function () {///Add Say () method
alert (this.name); Output New Object
}

For the above creation method, we can use the method of JSON (JavaScript Object notation) to abbreviate the following code:
Copy Code code as follows:

var newobject = {
Name: "New Object";
Say:function () {
alert (this.name);
}
};

We use JSON's data format to create a more complex object
Copy Code code 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 years old;

We can see that the code, created in JSON's data format, not only looks elegant.
The form of JSON is a list of items that are included with a large "{}" number, separated by commas "," and the item is a property name and attribute value separated by a colon ":". This is a typical dictionary representation and once again shows that the object in JavaScript is the dictionary structure. No matter how complex the object is, it can be created and assigned by a JSON code.
b, creating objects from constructors
Copy Code code 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 a new Objectfun () function that defines the properties and methods, where we can think of Objectfun as a class (a function is an object in JavaScript) and then instantiate an object from new. The NewObject object also has properties and methods in the parent class.
We can use the following code to detect that a function is actually an object:
Copy Code code as follows:

normal function
function say (s) {
alert (s);
}
Say ("HI");
Assigning properties to a Function object
Say.test = "It can work?";
alert (say.test); Output it can work?

How do you understand the above methods for creating objects? Let's look at the following code:
Copy Code code 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 as the This parameter newobject
Newobject.say ("Zhangchen");//output Zhangchen

First, create a NewObject object, NewObject call the Objectfun function as the this argument. With so much to say, we can use Objectfun as a constructor.
--------------------------------------------------------------------------------------------------------------- ----------------------------------
For the rest, you can refer to Li Shi's comprehension of 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.