Home >Web Front-end >JS Tutorial >Creation and access of javascript objects_javascript skills

Creation and access of javascript objects_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:11:341212browse

JavaScript rarely makes people think of its object-oriented features. 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 objects in most 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.

Create 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 any data type, or a function or other object. 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:

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 to foo,
Get its members and assign values ​​through foo.prop1, where {} is the representation method of object literal, or you can use var foo = new Object() to explicitly create an object.
1. Use associative arrays to access object members
We can also use the associative array pattern 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 is equivalent to an associative array reference, which means that any object (including
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 only gives you an understanding of the definition of JavaScript objects. When actually using it, we will use 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's initializer. Note that when using an initializer, whether the object property name is quoted is optional. 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 implementation method of creating and accessing objects in JavaScript. I hope it will be helpful to everyone's learning.

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