"JavaScript advanced Programming" Reading notes-(3) citation types

Source: Internet
Author: User

ECMAScript is an object-oriented language technically, but it does not have the basic structure of classes and interfaces supported by the traditional object-oriented language. Although reference types look similar to classes, they are not the same concept. A reference type is sometimes also defined as an object because it describes the properties and methods that a class of objects have.

Object type

There are two ways to create an object instance. The first is to use the new operator followed by the object constructor, and the other is to use the object literal notation. That is, the new Object () is equivalent to {}, and the sample code looks like this:

Use the new operator followed by the object () method
var person=New Object ();
Person. name= ' BINGHUOJXJ ';
person.age=29;
Using the object literal method
var car={
    name: ' BMW ',
    Num:4
        };
Accessing an object's properties can also use square brackets notation
Alert (' person.age= ' +person["age"]+ ' \ r \ n ' + ' car. name= ' +car['name"]);

The output results are as follows:

Array Type

Arrays in ECMAScript are quite different from those of most other languages. Specific features are as follows:

    1. An array is an ordered list of data, but each item of an array can hold any type of data;
    2. The size of the array can be dynamically adjusted.

There are two ways to create an array: By using the array constructor and array literal notation. The sample code looks like this:

Directly using the array constructor, passing the number of elements
var colors=NewArray(4);
Passing array elements directly using the array constructor
var nums=New Array (' 34 ');
Directly using the array constructor, you can add no new operator
var names=Array(' jxj ', ' WSC ', ' my ');
Using array literals notation
var types=['number ', 'String', ' Undefined ', ' Null ', ' Object ', 'Boolean'];

The ECMAScript array provides the same behavior as a stack (a LIFO data structure, abbreviated as LIFO), and the corresponding methods are the push () method and the Pop () method, respectively. The push () method can accept any number of arguments, add them to the end of the array one by one, and return the length of the modified array, while the pop () method removes the last item from the end of the array, reduces the length of the array, and returns the removed item. The data structure that corresponds to the stack is the queue (the queue adds data at the end, deletes the data from the front end), and the method of implementing the operation is the push () method and the Shift () method. The push () method is the same as the shift () method, which removes the first item from the array, reduces the length of the array, and returns the item that is removed. The sample code looks like this:

var names=[' Jxj ', ' lhy ', ' my ';
Add an item to the end of the array by the Push method
var currentlength=names.push (' mht ');
Alert (' Current array length: ' +currentlength ');
To delete an item at the end of an array by using the Pop method
var delval=names.pop ();
Alert (' Current array element: ' +names+ ' is deleted element ' +delval ');
To delete the first item of an array by using the Shift method
Delval=names.shift ();
Alert (' Current array element: ' +names+ ' is deleted element ' +delval ');
Add any item at the beginning by using the Unshift method and return the length of the modified array
Currentlength=names.unshift (' LRP ', ' Zy ');
Alert (' Current array length: ' +currentlength ');
Alert (names);

It is often used in array operations to define the position of an element in an array, which corresponds to the two-position method: IndexOf () and LastIndexOf (). where the IndexOf () method starts looking backwards from the beginning of the array (position 0), and the LastIndexOf () method looks forward from the end of the array. The item to be found must conform to the same conditions (= = =) and return-1 if no matching Item method is found. The sample code looks like this:

var numbers=[1,2,3,4,5,4,3,2,1,0];
Test IndexOf method
Alert (Numbers.indexof (3));
Test LastIndexOf method
Alert (Numbers.lastindexof (3));
A condition where the test element does not exist
Alert (Numbers.indexof (7));

Date Type

Here are 4 ways to create a Date object, as shown in the following code:

var New Date ();
var New Date (milliseconds);
var New Date (datestring);
var New Date (year, month, day, hours, minutes, seconds, milliseconds);

To display the current date, use the "2014-09-03 15:12:24" format, which corresponds to the following code:

function Curenttime ()
    
        var New Date ();
        var year = Now.getfullyear ();      //year
        var month = Now.getmonth () + 1;     //month
        var day = Now.getdate ();            //day
        var hh = now.gethours ();            //Time
        var mm = Now.getminutes ();          //min
        var second=now.getseconds ()         //sec
       
        var clock = year + "-";
        if (Month < 10)
            Clock + = "0";
        Clock + + month + "-";
        if (Day < 10)
            Clock + = "0";
        Clock + + day +"";
        if (HH < 10)
            Clock + = "0
        Clock + + hh + ":";
        If
        Clock + = mm+ ":";
        If
        Clock + = second;
        Return
    

Because the method of the Date object is much more, you should pay particular attention to the range of values returned by the method when using the method of the Date object. The date method in JavaScript differs greatly from the C # method, noting that the method returns the range of values and translates it into a daily-use format.

function type

The contents of the function are many, and the following points need to be understood are listed below:

    1. Functions are objects, each of which is actually an instance of a function type;
    2. A function name is essentially a pointer to a function. The function name is a pointer, and the function name plus () indicates the calling function;
    3. function is not overloaded;
    4. The function name itself is a variable, so the function can be used as a value;
    5. The callee property of the Agruments object inside the function points to the pointer that owns the Agruments object;
    6. function has two non-inherited methods of apply () and call (), which can expand the scope of the function to survive;
    7. The prototype property of a function plays an important role in implementing custom reference types and inheritance.

"JavaScript advanced Programming" Reading notes-(3) citation types

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.