Javascript Study Notes [7]-built-in objects

Source: Internet
Author: User

I went back to the countryside last night, so I didn't have time to write a blog. Now let's continue. Today I learned the object model enhanced by ecmascript 5 and some built-in JavaScript objects, to sum up. Let me explain it first. I am also a cainiao. it's not long before I learned it. Let's discuss it together. If you have any questions about blog writing, please help me out in time.

7.1 ecmascript 5 enhanced Object Model

After reading it, I don't know how to use it. I feel that everything I talk about is a good theoretical thing. It should be less practical. Focus:

Ecmascript 5 enhances its control over the object model. With the new object model, you can control whether individual attributes allow reading, writing, deleting, and enumeration, you can even control whether the object allows adding or deleting attributes so that the object can be sealed. -------- It's too literary. A cainiao like me cannot use such advanced things at present.

You can use object. defineproperty () to define attributes and change the key value of the descriptor. Syntax:

Object. defineproperty (OBJ, prop, DESC );

OBJ: object. this parameter is specified as the prototype object of this class.

Prop: string used to specify the property name

Desc: Description of attributes

 Function  Person () {} object. defineproperty (person. prototype, "Name" , {Value: "Tony" ,  //  These attribute definitions are true by default. Writeable: True  , Enumerable:  True , Retriable:  True  });  //  BelowCodeYou can get the attribute value Tony.  VaR Person = New  Person (); document. Write (person. Name ); 

 

Next, you can define multiple attributes at a time and use the object. defineproperties () method. Object. defineproperties (OBJ, props)

Next we will introduce several enumeration attributes, that is, a little bit. I have an impression that I will check them on the site if I want to use them.

Object. Keys (OBJ) to obtain the attribute name, that is, the name of the code above.

Object. getownpropertynames (OBJ) to list all enumeration and non-enumeration attributes. This method cannot be forward compatible.

Sealed object and dynamic object,The sealed object can only have a fixed set of attributes and methods obtained from the class definition during instantiation. Other attributes and methods cannot be added at runtime. By default, all objects created in JavaScript are dynamic objects, that is, other attributes and methods can be added to objects at runtime. The specific implementation will not be mentioned. However, this method requires a high level and can only be seen in use strict mode. Currently, only firefox4 supports this method. The compatibility is not good.

7.2 string class

Introduce some things that are often used in actual programming.

7.2.1 regular expression syntax

I believe you have certainly seen a variety of regular expressions, especially during verification, but you don't know what it means. That's a little more popular here.

Javascript uses the Regexp class to represent regular expressions. There are three creation methods and the simplest matching code:

View code

 VaR Re =/AB + C/ //  (Surrounded by two // values without quotation marks)  VaR Re = New Regexp ("AB + C ") //  Create a Regexp object  VaR Re = New Regexp ( New Regexp ("AB + C ")); //  Use one Regexp object to create another Regexp object  VaR Re =New Regexp (/AB + C /); //  The method above.  The following code matches a continuous string ABC in a string.  VaR Pattern = "ABC" ;  VaR Re = New  Regexp (pattern );  VaR Ostring = "abcefg" Optional document.write(re.exe C (ostring ));  //  Returns ABC if the matching is successful; returns NULL if the matching fails. 

 

In JavaScript, use the exec () and test () Methods of the Regexp class to perform regular expression matching. Exec is embodied in the Code above. Test () is to find whether a string matches the specified mode. If yes, true is returned. Otherwise, false is returned. Below I will post several articlesArticleIndex:

Proficient in regular expressions in Javascript

Javascript Regular Expression

Classic Javascript Regular Expression

7.2.2String:String ("Tony") and string object (new string ("ABC") can call all the members of the string class, but the two are different, the javascript interpretation engine automatically converts it to a temporary String object, calls its method, and finally clears the temporary object. Unless you really need to use a String object (because the string object can have unconventional behavior), we recommend that you use string text. For the string method, see this blog post: The javascript_string function. Let's take a look at some interesting code-create a typewriter:

View code

<! Doctype HTML>  //  In fact, it is constantly updating the displayed text. Each time you add one more word, it looks like a printer.  VaR Typingtest = "put a piece of text in it and show it as a typewriter" ;  VaR Count = 0 ;  VaR Oblock = Document. getelementbyid ("test" );  Function  Type (){ If (Count <= Typingtest. Length) {oblock. innerhtml = Typingtest. substring (0, count ); //  Count adds Count ++ ;}  Else  {Window. clearinterval (intervalid );}}  VaR Intervalid = Window. setinterval (type, 200 ); //  This function is called every 200 milliseconds. </SCRIPT> </body> 

 

You can also use this method to create an HTML string. See the code section below.

View code

 
VaRMystring = "view the first part of content"; Document. writeln (mystring. Anchor ("Section1"));//This will create the following named anchor<A name = "Section1"> view the first part </a>VaRMysite = "My Bolg";VaRMysiteurl = "http://www.cnblogs.com/tonylp"; Document. Write ("Welcome" +Mysite. Link (mysiteurl ));//This creates the following hyperlinkWelcome to <a href = "http://www.cnblogs.com/tonylp/"> my Bolg </a>

 

7.3 array, multi-dimensional array and composite array (hash ing)

Array is a complex data type in Javascript, and array is also a built-in core class. An array is an instance of the array class, you can use methods and attributes of the array class to operate arrays. Elements in the Javascript array do not have to be of the same data type and can be mixed.

There are three methods to construct the array class: New array (); new array (length); new array (ele1, ele2, ele3 );

There are some array APIs for processing array JavaScript Array APIs, and some new methods for ecmascript, composite arrays, cloned arrays, and hash tables. I will talk about them later, today, I am not in the status. I have a runny nose and it is hard to die.

7.4 use the date class to process Date and Time

The date class is used to obtain the JavaScript code running.ProgramThe date and time of the operating system. You can remember the following code frequently, and use Baidu for more details.

View code

 
VaRMydate =NewDate ();//Get time Fri Feb 01 2013 16:01:15 GMT + 0800 (Chinese Standard Time)VaRMyyear = mydate. getfullyear ();//Obtain the four-digit year 2013VaRMymonth = mydate. getmonth () + 1;//Starting from 0, so we need to add 1 and Output 2.VaRMyday = mydate. getdate ();//Returns the number of days of the date object, and getday returns the number of weeks. Output 1

 

Then, you can reorganize the date format by yourself and send it back to the background in this form to return it back to the database.

7.5 other built-in JavaScript classes (core objects)-Boolean, number, math, function, and arguments classes

Boolean and number are nothing to talk about. Math also involves some methods, such as triangular cosine, sine, absolute value, square root calculation, and natural logarithm calculation. To be honest, javascript's digital processing capabilities are quite weak. You just need to know it.

Function classes include call (), apply (), and bind () methods. Call () methods are generally replaced.

The arguments class and the arguments attribute. The arguments class is used to store and access function parameters. It is located inside the function body and can be accessed using the arguments attribute of the function class. The previous Code has used arguments, which is quite useful. Arguments. length is used to determine the length. Arguments [N] gets parameters.

Let's write so much today. There have been a few things over the past few days, and blog updates are not scheduled. It is expected that BOM and Dom will be started later.

All of the above are personal originals. Please refer to the original link when you reprint it:Http://www.cnblogs.com/tonylp

 

 

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.