Creation and access of javascript objects, creation of javascript objects

Source: Internet
Author: User

Creation and access of javascript objects, creation of javascript objects

JavaScript rarely reminds people of its object-oriented features. Some people even say it is not an object-oriented language because it has no class. Yes, JavaScript really does not have classes, but JavaScript is an object-oriented language. JavaScript only has objects, and objects are objects, not instances of classes.
Because most objects in object-oriented languages are class-based, some people often confuse the concept of class instances and objects. Objects are examples of classes, which are true in most languages, but not in JavaScript. Objects in JavaScript are prototype-based.

Create and access

Objects in JavaScript are actually an associated array composed of attributes. attributes are composed of names and values. The value type can be any data type, or functions and other objects. Note that JavaScript has the feature of functional programming, so a function is also a variable, and most of the time it is not distinguished from the general data type.

In JavaScript, you can use the following method to create a simple object:

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, an object is created through var foo = {}; and Its Reference is assigned to foo,
Use foo. prop1 is used to obtain its members and assign values. {} is the literal representation of the Object. You can also use var foo = new Object () to explicitly create an Object.
1. Access Object members using Correlated Arrays
We can also use the associated array mode to create objects. The above code is changed:

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

In JavaScript, the use of the period operator is equivalent to the associated array reference, that is, any object (including
These two modes can be used. When we do not know the object attribute name, variables can be used as the index of the associated array. For example:

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

2. Create an object using the object initialization Tool
The above method only gives you an understanding of the definition of JavaScript objects. When we are actually using it, we will adopt the following more compact and clear method:

var foo = {  'prop1': 'bar',  prop2: 'false',  prop3: function (){  return 'hello world';  }};

This defined method is called the object initiator. Note: When initializing an object, it is optional to check whether the object property name is enclosed by quotation marks. Unless the property name contains spaces or other characters that may cause ambiguity, quotation marks are not required.

The above describes how to create and access objects in javascript. I hope it will be helpful for your learning.

Articles you may be interested in:
  • Create objects in JS (several common methods)
  • Js object external access or call problems
  • Create an object using JavaScript
  • Three methods for creating objects in JavaScript
  • Ajax asynchronous data acquisition implementation code (js creates ajax objects)
  • Javascript converts a string to a dom object (dynamically creates a dom string)
  • Create a custom Object in Javascript. Create an Object instance. Add attributes and methods.
  • Examples of how to create objects in js
  • Sample Code for creating a createXmlHttpRequest object using javascript
  • Jquery creates a JSON object using a JSON string

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.