Home  >  Article  >  Web Front-end  >  Creation and access of JS objects

Creation and access of JS objects

怪我咯
怪我咯Original
2017-06-29 10:29:351231browse

This article mainly introduces the creation and access implementation methods of javascript objects in detail. Interested friends can refer to

JavaScript, which rarely reminds people of It has object-oriented features, and some people even say that it is not an object-oriented language because it has no classes. Yes, JavaScript doesn't really 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 based on classes, people often confuse the concepts of class instances and objects. Objects are instances of classes, which is true in most languages, but not in JavaScript. Objects in JavaScript are prototype-based.

Creation and access

An object in JavaScript is actually an associative array composed of attributes. The attributes consist of names and values. The type of the value can be anyData type, or functions and other objects. Note that JavaScript has the characteristics of functional programming, so functions are also types of variables and most of the time do not need to be distinguished from general data types.

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

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 create an object through var foo = {}; and assign its reference For foo,
obtain its members and assign values ​​through foo.prop1, where {} is the representation method of object literals. You can also use var foo = new Object() to explicitly create an object.
1. Use associative array to access object members
We can also use associative array mode to create objects, 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 are equivalent, which means that any object (including the
this pointer) can use both modes. The advantage of using an associative array is that when we don't know the attribute name of the object, we can use variables as the index of the associative array. For example:

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

2. Use object initializer to create objects
The above method is just to give you an understanding of the definition of JavaScript objects. When actually using it, we will use The following is a more compact and clear method:

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

This defined method is called the object's initializer. Note that when using an initializer, Object property names are optional in quotes. Unless there are spaces or other characters that may cause ambiguity in the property name, there is no need to use quotes.

The above is the detailed content of Creation and access of JS objects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn