JavaScript Object Concept Encyclopedia

Source: Internet
Author: User
Tags constructor

This article describes almost all the basic concepts of objects, what objects are, how objects are created, how objects are set up and read, how to delete attributes, constructors, object prototypes, parent classes, subclasses, inheritance, and so on.

1. The object

* Objects are composite data types that centralize multiple data values in one cell and run using a name to access those values. Another way to interpret an object is that the object is a unordered collection of attributes, each with its own name and value. Named values stored in an object can be original values such as numbers and strings, or objects.

*/

var o = new Object ();

2. Object Direct Volume Create object

* The direct amount of the object consists of a list of attribute descriptions, which are enclosed in curly braces, where the property descriptions are separated by commas.

* Each property description in the object's direct volume is made up of a property name plus a colon and a property value.

*/

var Zhangsan = {name: "Zhangsan", Age:34, married:true};

Window.alert (typeof Zhangsan); Output Object

3. Read and Set properties

* The properties of the object are read and set by the dot operator. The new properties of the object can be created directly.

*/

Window.alert (Zhangsan.car); Output undefined

Zhangsan.car= "BMW"; Directly assigned value

Window.alert (Zhangsan.car); Output BMW

4. Enumeration of attributes

* The order is not guaranteed to be fixed by enumerating each property through a for loop.

* Note that the name of the attribute is not a property value.

*/

var values = "";

for (Var v in Zhangsan) values + = v+ "\ n"; Enumerate each property

Window.alert (values);

5. Deletion of properties using delete, such as delete Zhangsan.car

*/

6. Constructor function

The * constructor is a JavaScript function with two attributes:

* (1) It is called by the new operator;

* (2) to pass to it is an application of the newly created empty object, the reference as the value of this keyword, and it also has to initialize the newly created object appropriately.

* Remember: The constructor simply initializes the object and does not return the newly created object.

*/

function Rectangle (W, h) {

This.width = W;

This.height = h;

}

var ret1 = new Rectangle (2, 1);

Note how the constructor uses its arguments to initialize the properties of the object referenced by the This keyword

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.