Summary of JavaScript objects (1)

Source: Internet
Author: User

Summary of JavaScript objects (1)

In JavaScript, all objects except the five primitive types (numbers, strings, Boolean values, null, and undefined) are objects. Therefore, if you don't want to learn objects, how can you continue learning?

I. Overview

An object is a composite value that aggregates many original values or other objects and can be accessed through attribute names. The attribute name can be any string that contains an empty string. JavaScript objects can also be called a data structure, as we often hear about "hash)", "hash table hashtable)", and "dictionary) "," associate array associative array )".

Objects in JavaScript can be divided into three types:

① Built-in objects, such as arrays, functions, and dates;

② The Host object, that is, the host environment embedded by the JavaScript interpreter, such as a browser, is defined, for example, HTMLElement;

③ Custom object, defined by the programmer using code;

The attributes of an object can be divided into two types:

① Self-owned own property): attributes defined directly in the object;

② Inheritance property inherited property): attributes defined in the object's prototype object will be discussed in detail below the prototype object );

Ii. Object Creation

Since learning objects, how can we not understand how to create objects? The following basic questions may have been asked by the interviewees:

What are the two methods for creating JavaScript objects? Or: How to Create a JavaScript Object ?)

I have been asked this question twice. There are many online saying "two ways to create objects", but there are three methods as far as my books are concerned! Here we will talk about these three methods:

1. Direct object volume

A ing table composed of several name/value pairs. The name/value pairs are separated by colons, and the name/value pairs are separated by commas. The entire ing table is enclosed in curly brackets. The property name can be either a JavaScript identifier or a string's direct quantity. That is to say, the following two methods of creating object obj are exactly the same:

Var obj = {x: 1, y: 2 };
Var obj = {'X': 1, 'y': 2 };

2. Create an object through new

After the new operator is followed by a function call, that is, the constructor creates and initializes a new object. For example:

1 var o = new Object (); // create an empty Object, same {}
2 var a = new Array (); // create an empty Array, same []
3 var d = new Date (); // create a Date object that represents the current time

Let's talk about the constructor.

3. Object. create ()

ECMAScript5 defines a method named Object. create (), which creates a new Object. The first parameter indicates that the prototype Object of this Object does not seem to have been released... The second optional parameter is used to further describe the object attributes. The second parameter is described below because the third method is defined in ECMAScript5, so we used to talk about the two methods for creating objects? I think this is the reason ). This method is easy to use:

1 var o1 = Object. create ({x: 1, y: 2}); // The Object o1 inherits the attributes x and y
2 var o2 = Object. create (null); // The Object o2 has no prototype.

The following three are exactly the same:

1 var obj1 = {};
2 var obj2 = new Object ();
3 var obj3 = Object. create (Object. prototype );

To explain why the three methods are exactly the same, let's first explain the prototype objects in JavaScript and let the guest wait !), Remember a great God said:

Javascript is an object-based language. Almost everything you encounter is an object. However, it is not a real object-oriented programming language (OOP), because its syntax does not contain class classes ).

Object-Oriented Programming Language JavaScript, no class !!! So how does it implement inheritance? Yes, it is through the prototype object. Basically, every JavaScript Object except null is associated with another object. Another object is a prototype object, which can also be referred to as a prototype. It is not as complicated as you think, it is only an object ). Each object inherits attributes from the prototype object, and the value of the prototype attribute of an object is automatically generated by default when the object is created and does not need to be displayed with custom attributes) is the prototype object of this object, that is, obj. prototype is the prototype object of object obj.

The prototype object first mentioned this and then went back to the above question. With the understanding of the prototype object, the following JavaScript language does not need to be explained too much:

① All prototype objects created directly through objects are Object. prototype objects;

② The prototype Object of the Object created by the keyword new and the constructor is the value of the constructor prototype attribute. Therefore, the prototype of the Object created by the constructor Object is Object. prototype;

The meaning of the first parameter of Object. create () in the third method for Object creation is also supplemented.


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.