Common javascript Datasets

Source: Internet
Author: User

I. Array creation method var arrayObj = new Array (); // create an Array var arrayObj = new Array ([size]); // create an Array and specify the length, note that it is not the upper limit. It is the length var arrayObj = new Array ([element0 [, element1 [,... [, elementN]); // create an array and assign values. Although the length is specified in the second method, but in fact, in all cases, the array is longer, that is, even if the length is set to 5, the elements can still be stored outside the specified length. Note: The length will change accordingly. Basic operation var testGetArrValue = arrayObj [1]; // obtain the array element value arrayObj [1] = "this is a new value "; // assign a new value to the array element. 2. Object creation method. 1. Enclose var emptyObj ={}; var myObj = {'id': 1, // attribute names are enclosed in quotation marks. attributes are separated by commas: 'name': 'myname'}; // var m = new myObj (); // not supported 2. Use the function keyword to simulate class function myClass () {this. id = 5; this. name = 'myclass'; this. getName = function () {return this. name ;}} var my = new myClass (); alert (my. id); alert (my. getName (); Use function to create an object constructor var Person = function (name) {// an anonymous function, and assign this function to a Person variable, at this time, Person becomes a class this. name = name;} function Person (name) {// directly defines a function called Person to represent the Person class this. name = name;} Person. prototype = {// define the prototype field printName: function () {// define a print function alert (this. name) ;}} after you declare a class through a function, you can instantiate the class through the new operator. In this way, you can call the class member function to complete your logic var person = new Person ("Joe Smith"); // use the new operator to create a Person instance, and assign it to the variable person. printName (); // person can be considered as a reference of an instance, so you can call the member function in the Person class through this reference.

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.