JavaScript object and Array usage

Source: Internet
Author: User

Reference type: A reference type is a data structure used to organize data and functionality together. The value of a reference type is an instance of a reference type.

One, Object

The object in ECMAScript is actually a combination of data and functions.

The object type is actually the basis of all its instances, in other words,all properties and methods of the object type are also present in more specific objects.

Constructor Property: This property holds the function that is used to create the current object, the constructor of the current object, and the constructor of the object type is Object ()

hasOwnProperty method: Used to check whether a given property exists in an instance of the current object , not in the prototype of the instance

isprototypeof method: Used to check whether an object Object1 (parent) exists in the prototype chain of another object Object2 (child)

ToString () Method: Returns the string representation of an object

ValueOf () Method: Returns the representation of a string, numeric value, or Boolean of an object

Two ways to create:

var New Object () var o =$, Name:  "Xiaoming"}

Operation Method:

function () {}; Delete: Delete   = at the same; check: o.name; O[name];    var other = O;  Other.age = 24; Console.log (O.age) //The same object is pointed to by both O and other. 

Prototypes: Each object is connected to a prototype object and can inherit properties and methods from it. All objects created by object literals are connected to the object.prototype.

When we try to get the property of an object, if we can't get it from that object, JS will try to get the property value up from its prototype object , until it reaches the end of Object.prototype, and if not found, that is undifined.

The prototype connection is not functional at the time of the update . When we make a change to an object, we don't touch the object's prototype .

Case:

Object.prototype.sex = "male";    var o = {  };console.log (o);   //  {age:22}Console.log (o.sex);  O.sex  //"male"var result = O.hasownproperty ("Age"); // true  var result = O.hasownproperty ("Sex"); // false


Two, Array

var arr = ["A", "B", "C", "D", "E"] js in each of the array can hold any type of data;

Operation Method:

var arr = ["A", "B", "C", "D", "E"];

1. Conversion method

var result = Arr.tostring (); // "A,b,c,d,e"  var result = Arr.join (); //  "A,b,c,d,e" var result = Arr.join (""); The "ABCDE" join is split by default by ","

2. Stack method

Arr.push ("F");  Console.log (arr); // ["A", "B", "C", "D", "E", F "]    Arr.pop ();  Console.log (arr);  //["A", "B", "C", "D"]

3. Queue method

 Arr.unshift ("F");  Console.log (arr); // ["F", "a", "B", "C", "D", "E"]   arr.shift ();  Console.log (arr);  // ["B", "C", "D", "E"]  

4. Location method

var result = Arr.indexof ("a");  Console.log (result); // 0

5. How to:

//  Delete Console.log (arr);  ["A", "B", "C", "E"]arr.splice (// replace Console.log (arr);  ["A", "B", "C", "F", "E"]arr.splice (//  Insert Console.log (arr); ["A", "B", "C", "F", "D", "E"]var result = Arr.slice (2,4); // Latent copy console.log (result)//["C", "D"]

6. Sorting methods

var arr1 = [5,9,3,4,6];  var result = Arr1.reverse ();  Console.log (result); // [6,4,3,9,5]  var result = Arr.sort (function(A, b) {return -A-B});  Console.log (result); // [3,4,5,6,9]

7. Merging methods

var arr1 = ["A", "B"];  var arr2 = ["C", "D"];  var result = Arr1.concat (ARR2);  Console.log (result); // ["A", "B", "C", "D"]

Arrays are also reference types, and two variables point to the same array:

var arr1 = arr;  Arr1.push (1);  Console.log (arr); // ["A", "B", "C", "D", 1]

JavaScript object and Array usage

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.