JavaScript Learning notes 1-objects and arrays

Source: Internet
Author: User
Tags arrays constructor

1. Objects and Arrays

General statement

Objects and arrays are two important data types in JS, and they are different from basic data types such as strings and numbers: they do not represent a single value, they represent a collection of values.

An object is a collection of named values, and an array is a special object that is an ordered set of values.

1.1 Creating objects

Objects gather multiple data values in a single unit and allow them to be stored and retrieved by name.

The object definitions are generally as follows:

JS Code

var empty = {}
var point = {x:0,y:0}
var home = {
  “name”:”liugx”,
  “age”:29,
  “married”:false
  “email”:”liugx@sina.com”
}

The above wording we often use in the program, such as the function of the parameters as an obj to pass, such as the definition of an obj, assign attributes, as a set to use; For example, in the client JS new img OJB, assigned to its properties, such as left,top, etc., Again appendchild to document to display, such as often packaged in a JSON in the server, and then in the foreground of the returned string eval to obj, and then through the property to get the specific value ....

In the program, we often use the var o = new Object () to define an object, and then assign the corresponding property to this O. The object () here is a constructor, just as we often define in JS:

JS Code

function TrackRecordMgr(){}
TrackRecordMgr.prototype={
 doFun1:function(){
 },
 doFun2:function(){
 }
}

The above trackrecordmgr () is our custom constructor, just when we write JS, we often forget the image object's thought. The constructors that are built in JS are often:

JS Code

var a = new Array();
var d = new Date();
var r = new RegExp();

1.2 Object Properties

The following are the main attributes of object properties:

1, the properties of an object can be created by assigning it a value. This is something we often use in a program. Once you have created a property, you can change the value of the property at any time.

2, the object attributes are divided into two types of identifiers and strings, to understand the use of these two different aspects, especially the string array of the wording.

Using array notation to access the properties of an object is very flexible, why do you say so? Look at the following code:

JS Code

for(var i=0;i<5;i++){
 addresses = o[“addIndex”+i]+”,”
}

At the same time, we often pass an object as an argument to another object in the code, and another object is added to itself by traversing the property of the first object. You can only assign values by array notation because you cannot know all the property names of the first object at the time of the traversal:

JS Code

for(var name in obj1){
 if(obj1.name){
 ojb2[name] = obj1[name];
 }
}

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.