JS organization memo (03)-object Basics

Source: Internet
Author: User

Concept: an object represents an unordered set of named data.AttributeAttribute can be any type of data (array, function, object ...).

Functions in an object are usually calledMethod.

Type: the object is a composite data type. After typeof () operation, the returned type is object.

Features: The number of attributes can be infinite. The attribute name is an identifier or string, and the attribute value can be any type of data.

 

1. Object Creation

(1) Use the object direct volume. In this example, the object Stu is an object direct volume:

 <  Script  Type  = "Text/JavaScript"  Language  = "JavaScript"> VaR Stu = {ID: 12, // The attribute name is usually a JS identifier Name: "Object" ,"Age" : 20, // Attribute names can also be represented by strings Holobby :[ "Football" , "Drawing" , "Ming" ], // The property value can be of any type Other: {A: 1, B: "A sample" }}; Document. Write (STU. holobby ); // The result shows football, drawing, and Deming.  </  Script  > 
 
 
 

(2) use the new operator

Create a specific type of object using the new operator, for example:

 <  Script  Type  = "Text/JavaScript"  Language  = "JavaScript"> VaR O = New Object (); // Create an empty object  VaR A = New Array (1, 2, "ASD" ); // Create an array object VaR S = New String ( "A string" ); // Create a String object O. x = 123; O. Str = "ASD" ; // Add properties to object O. Here, the X and STR are identifiers. Document. Write (O. Str ); // Output "ASD"  </  Script  > 
 
Here, object (), array (), and string () are all part of the core JavaScript (hereinafter referred to as JS). In addition, date () and Regexp () are commonly used.
 
 

(3) create an object using a custom Constructor

To understand this method, we must first have an understanding of the constructor concept. It is not difficult to judge from the literal meaning that the type of constructor should be function ). In fact, the object, array, and string used in (2) above are all constructors. For example: typeof (object); typeof (array); typeof (string); The return type is function;

Constructors can be used to create objects. The type of the created object instance is object.

For example: typeof (o); typeof (a); typeof (s); the return type is object.

Here is a simple example to illustrate the creation method:

FunctionFN (){This. X ="X";}// Define a simple function FN
VaRFo =NewFN ();// Create an object instance fo of the constructor FN

Theoretically,Any function can be used as a constructor to create an object instance.The relationship between the function and the object is very subtle, because the current understanding of the function is not deep enough, it will be described in detail later.

If you are interested, take a look at this article.Article: Why is object. Prototype. isprototypeof (function) true?

 

2. Object Access

Javascript objects can be accessed in two ways: object. Property or object ["property"]. The biggest difference is that the property name of the former is the identifier, and the property name of the latter is a string.

An identifier is not a data type. It must be input word by word andProgramYou cannot operate on it, but the string is a type of JS data. Therefore, you can operate on it while the program is running.

Associative array)

If an object is in the format of object ["property"], it is often calledJoin ArrayIt is a data structure that allows dynamic association of any string and any type of data.

For example, when you want to read all the attributes of an object, it is not reasonable to use the dot "." operation, because the attribute name (identifier) after "." must know in advance how to spell it. "[]" Can be easily used.

<ScriptType= "Text/JavaScript"Language= "JavaScript"> VaROBJ = {ID: 12, Name:"Object","Age": 24,
 
Holobby :["Football","Drawing","Ming"],
Other: {A: 1, B:"A sample"}};For(PInOBJ) {document. Write (p +":"+ OBJ [p] +";");// Here p is a variable, regardless of which specific string it represents}</Script>

The displayed result is: ID: 12; Name: object; age: 24; holobby: football, drawing, locking Ming; Other: [object].

 

3. common and important attributes and methods of Objects

A clear concept: all objects in JS are inherited from the object class.

(1) constructor (constructor) attributes

Every object in JS has this attribute, which references the constructor of this object.

For example, VAR d = new date (); then D. constructor references the constructor date (that is, the expression D. constructor = date is true ).

Therefore, the constructor attribute helps determine the object type. For example:

If (typeof o = "object") & (O. constructor = Date ))

The instanceof operator mentioned in the previous article is also used to determine the object type. The preceding statement can also be as follows:

If (typeof o = "object") & (O instanceof date) // If O is an object instance of date, true is returned.

For more details about the constructor attributes, refer to this article: Add the following basics: javascript constructor and constructor attributes.

 

(2) isprototypeof () method

Note: If the isprototypeof () method belongs to the prototype object of the parameter, true is returned for this method.

Prototype is also a complex and important concept in JS. It may be hard to understand at the beginning, and it is not suitable for details here. For example:

<ScriptType= "Text/JavaScript"Language= "JavaScript"> VaRO ={}; alert (object. Prototype. isprototypeof (o ));// True: O. constructor = ObjectAlert (function. Prototype. isprototypeof (object ));// True: object. constructor = Function</Script>
 
For more information about the relationship between objects and functions, see why object. Prototype. isprototypeof (function) is true.
 
 
 
 
 
Objects are a very important concept in JS, and the subsequent learning is almost inseparable from objects. This section is written here first.
   

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.