JavaScript object in-depth learning summary

Source: Internet
Author: User
Tags object object

JavaScript object in-depth learning summary (1)


2015-09-29 09:27 clearbug Font Size:T | T

In JavaScript, except for the five primitive types (that is, numbers, strings, Booleans, null,undefined) are objects, so, do not learn how to continue to learn the object?


In JavaScript, except for the five primitive types (that is, numbers, strings, Booleans, null,undefined) are objects, so, do not learn how to continue to learn the object?

I. Overview

An object is a composite value that aggregates many values (original values or other objects) and accesses them through property names. The property 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)", "Hash List (hashtable)", "Dictionary (Dictionary)", "Associative array (associative array)".

Objects in JavaScript can be divided into three categories:

① built-in objects such as arrays, functions, dates, etc.;

② host objects, which are defined by a host environment (such as a browser) embedded in the JavaScript interpreter, such as HtmlElement;

③ custom objects, which are defined by the programmer in Code;

The properties of an object can be divided into two categories:

① properties (Own property): properties defined directly in the object;

② Inheritance Properties (inherited property): Properties defined in the object's prototype object (details are described below about the prototype object);

Two. Creation of objects

Since the learning object, how can not understand how to create objects? The students at the front-end of the interview may have been asked this basic question:

What are the two ways to create JavaScript objects? (or: Talk about how to create JavaScript objects?) )

I was asked two times about this question. "Two ways to create objects" there are many online, but there are three ways I can read books. Let's talk about these three ways specifically:

1. Object Direct Volume

The direct amount of the object is a mapping table of several name/value pairs, the middle of the name/value pair is separated by a colon, and the name/value pairs are separated by commas, and the entire mapping table is enclosed in curly braces. The property name can be either a JavaScript identifier or a string literal, which means that the following two creation objects obj are written exactly the same way:

var obj = {x:1, y:2};
var obj = {' x ': 1, ' Y ': 2};

2. Creating an object from new

The new operator follows a function call, which is a constructor that creates and initializes an object. For example:

1 var o = new Object (); Create an empty object, as with {}
2 var a = new Array (); Create an empty array, as in []
3 var d = new Date (); Creates a Date object that represents the current time

Say something about the constructor later.

3.object.create ()

ECMAScript5 defines a method named Object.create () that creates a new object, where the first parameter is the prototype object of the object (as if the prototype object has not been interpreted ...). The Second optional parameter is used to further describe the properties of the object, and the second argument (since this third method is defined in ECMAScript5, so it is common to say that the two methods of creating objects are used before). Personally think it should be the reason). This method is simple to use:

1 var O1 = object.create ({x:1, y:2}); Object O1 inherits properties X and Y
2 var O2 = object.create (null); Object O2 No prototypes

The following three kinds are exactly the same:

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

To explain why these three ways are exactly the same, let's first explain the prototype object in JavaScript (hey, let guest Nagyu wait!). ), remember a great God said:

JavaScript is an object-based (object-based) language, and everything you encounter is almost always an object. However, it is not a real object-oriented programming (OOP) language because it has no class (class) in its syntax.

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, and the "Other" object is the so-called prototype object (the prototype object can also be abbreviated as a prototype, which is not as complex as it is imagined, and it is just an object). Each object inherits properties from the prototype object, and the value of the prototype property of an object (which is automatically generated by default when the object is created and does not need to be displayed) is the prototype object of this object, that is, Obj.prototype is the prototype object of the object obj.

Prototype object first of all, back to the question above, with the understanding of the prototype object, the following is not required to explain too much JavaScript language provisions:

① the prototype object of all objects created by the direct amount of objects is the Object.prototype object;

② the prototype object of an object created by the keyword new and constructor is the value of the constructor prototype property, so the prototype of the object created by the constructor object is Object.prototype;

Now also complements the third method of creating an Object Object.create () The meaning of the first parameter.

12 3 4 5 Next >> view full text Content Navigation
1th page: Overview | creation of Objects Page 2nd: Query and settings for properties | Accessor properties
3rd page: Delete Attributes | Attributes of a property 4th page: Detection Properties | Enumeration properties
Page 5th: Three special properties of objects | Serialization of objects

JavaScript object in-depth learning summary

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.