Basic of javascript object-oriented technology (2)

Source: Internet
Author: User

I have read many introductionsJavascriptThe article on object-oriented technology is dizzy. Why? It's not because it's not well written, but because it's too esoteric. The objects in javascript haven't clearly explained what's going on. As soon as they come up, they go straight to the topic, class/Inheritance/prototype/private variable. As a result, after reading it for a long time, I had a rough understanding and had a good aftertaste. It seems that I did not understand anything.

This article is written in chapter 7, 8 and 9 of <javascript-the definitive guide, 5th edition>, I will try to describe the javascript object-oriented technology (Object/array-> function --> class/constructor/prototype) according to the structure of the original book ). I will attach the original English statement for your reference.

If no description is provided, all the English statements (except the program body) in the text are derived from <javascript-the definitive guide, 5th edition>.

Array

As we have mentioned, objects are unordered data sets, while arrays are ordered data sets. Data (elements) in arrays are accessed through indexes (starting from 0, the data in the array can be any data type. the array itself is still an object. However, due to many features of the array, the array and the object are usually differentiated by the following (Throughout this book, objects and arrays are often treated as distinct datatypes. this is a useful and reasonable simplification; you can treat objects and arrays as separate types for most of your JavaScript programming. to fully understand the behavior of objects and arrays, however, you have to know the truth: an array is nothing more than object with a thin layer of extra
Functionality. You can see this with the typeof operator: applied to an array value, it returns the string "object". -- section7.5 ).

To create an Array, you can use the "[]" operator or the Array () constructor to create a new one.

Js Code

 
 
  1. Var array1 = []; // create an empty array
  2. Var array2 = new Array (); // create an empty Array
  3. Array1 = [1, "s", [3, 4], {"name1": "NAME1"}]; //
  4. Alert (array1 [2] [1]); // 4 access the array elements in the array
  5. Alert (array1 [3]. name1); // NAME1 access the object in the array
  6. Alert (array1 [8]); // undefined
  7. Array2 = [,]; // if no value is specified and only a comma is entered, the element at the corresponding index is undefined.
  8. Alert (array2.length); // 3
  9. Alert (array2 [1]); // undefined

When using new Array () to create an Array, you can specify a default size, where the value is undefined, and you can assign values to them later. however, the length of the array in javascript can be changed at will, and the content in the array can also be changed at will.

Therefore, the initialization length does not actually have any binding force on the array. for an array, If you assign a value to an index that exceeds its maximum length, the length of the array is changed, and the index that does not assign a value is assigned undefined. See the following example.

Js Code
 

 
 
  1. Var array = new Array (10 );
  2. Alert (array. length); // 10
  3. Alert (array [4]); // undefined
  4. Array [100] = "100th"; // This operation changes the length of the array and sets the value of the 10-99 index to undefined.
  5. Alert (array. length); // 101
  6. Alert (array [87]); // undefined

You can use the delete operator to delete the elements of an array. Note that this deletion only sets the elements of the array at this position as undefined, and the length of the array has not changed. we have used the length attribute of the array. The length attribute is a read/write attribute, that is, we can change the length of the array by changing the length attribute of the array. if length is set to a value smaller than the length of the array, the index value greater than length-1 in the original array will be deleted. if the length value is greater than the length of the original array, the value between them is set to undefined.

Js Code

 
 
  1. Var array = new Array ("n1", "n2", "n3", "n4", "n5"); // array of the Five Elements
  2. Var astring = "";
  3. For (var I = 0; I <array. length; I ++) {// loop array element
  4. Astring + = array [I];
  5. }
  6. Alert (astring); // n1n2n3n4n5
  7. Delete array [3]; // delete the value of an array element
  8. Alert (array. length + "_" + array [3]) // 5_undefined
  9. Array. length = 3; // reduce the length of the array
  10. Alert (array [3]); // undefined
  11. Array. length = 8; // extend the length of the array
  12. Alert (array [4]); // undefined

For other methods of arrays, such as join/reverse, we will not give an example here.

Through the above explanation, we know that the object's attribute value is obtained by the attribute name (string type), and the elements of the array are indexed (integer 0 ~~ 2 ** 32-1) to get the value. The array itself is also an object, so the object attribute operation is also fully suited to arrays.

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.