Techniques for creating and accessing JavaScript objects _javascript

Source: Internet
Author: User

JavaScript, which rarely makes people think of its object-oriented features, is even said to be not an object-oriented language because it has no classes. Yes, JavaScript really doesn't have a class, but JavaScript is an object-oriented language. JavaScript has only objects, objects are objects, not instances of classes.
Because most objects in object-oriented languages are based on classes, people often confuse the concepts of class instances with objects. An object is an instance of a class, which is true in most languages, but does not apply in JavaScript. The objects in JavaScript are based on prototypes.

Create and access

An object in JavaScript is actually an associative array of attributes consisting of a name and a value, the type of which can be any data type, or functions and other objects. Note that JavaScript has functional programming features, so a function is also a variable, most of the time without the distinction of a typical data type.

In JavaScript, you can create a simple object in the following ways:

var foo = {};
foo.prop_1 = ' bar ';
Foo.prop_2 = false;
Foo.prop_3 = function () {return
' Hello World ';
Console.log (Foo.prop_3 ());

In the above code, we pass var foo = {}; An object is created and its reference is assigned to Foo,
The FOO.PROP1 is used to get its members and assign values, where {} is a representation of the literal amount of the object, or you can explicitly create an object with the var foo = new Object ().
1. Accessing an object member using an associative array
We can also use the pattern of associative arrays to create objects, and the above code is modified to:

var foo = {};
foo[' prop1 ' = ' bar ';
foo[' prop2 '] = false;
foo[' prop3 ' = function () {return
' Hello World ';

In JavaScript, using the period operator and associative array references is equivalent, meaning that any object, including
This pointer) can be used in both modes. The advantage of using associative arrays is that when we do not know the object's property name, we can use the variable as the index of the associative array. For example:

var some_prop = ' prop2 ';
Foo[some_prop] = false;

2. Creating objects using the object initializer
The above approach just gives you an idea of what a JavaScript object is, and when it is actually used, we'll take the following more compact approach:

var foo = {
  ' prop1 ': ' Bar ',
  prop2: ' false ',
  prop3:function () {return
  ' Hello World ';

This defined method is called an object initializer. Note that when using an initializer, it is optional to enclose an object property name, unless there are spaces in the property name or other characters that may cause ambiguity, and it is not necessary to use quotation marks.

The above is JavaScript to create and access to the object of the implementation method, I hope to help you learn.

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: 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.