JS custom objects, regular expressions, some of the knowledge points in jquery

Source: Internet
Author: User

One: Custom objects

1. Basic Concepts:
① object: A collection that contains a series of unordered properties and methods.
② Key-value pairs: The data in an object exists in the form of a key-value pair, with a key value.
③ Property: A series of variables that describe the characteristics of an object. [Variables in Object]
④ method: A series of methods that describe the behavior of an object. [Functions in Objects]

2. Declaration of the object:

① literal declaration: Var obj={};

②new keyword: var lisi = new Object ();

3. How to read and write properties and methods in an object:

① through. Operator: Object inside: this. Property name this. Method name ();
External object: Object name. Name of the property name. Method Name.

② through ["Key"] Call: Object name ["Property name"]; Object name ["Method name"] ();

③ deleting an object's properties and methods: Delete Object name. Property/Method name.

Example: Creating an object includes the name of a person, math, language, English score (keyboard input)

The subject of a person's name, achievement as an element of an object, each person as an array of elements to do

var classes= {
person:[], containing an array of student's
addperson:function () { The function that is called when you select the Enter Student Information option
newname=prompt ("Please enter student name:"); Enter information
newmeth=prompt ("Please enter Math score:");
newchinese=prompt ("Please input language score:");
newenglish=prompt ("Please input English score:");

if ((parsefloat (newmeth) | | | Parsefloat (Newmeth) ==0) &&
(parsefloat (Newchinese) | | Parsefloat (Newchinese) ==0) &&
(parsefloat (newenglish) | | Parsefloat (newenglish) ==0) { Determine if the input is legal

var student=new Object (); Create a new object, use Student.name=newname and so on to put newname, etc. into this object.

newsum=parsefloat (Newmeth) +parsefloat (Newchinese) +parsefloat (newenglish);


This.person.push (student); add each student to the person[] array in a this.person.push (student) way
This.person.sort (function (b) {
return a.sum-b.sum; Sort by the size of the value of the sum of student
Console.log ("Success in score Entry");
Console.log ("Score entry failed, input cannot be empty.") ")
showperson:function () { function called when selecting the Print Student Information option
Console.log ("\t\t Jerry Education Student Performance Show");
Console.log ("serial number \ t name \ t language \ t math \ t english \ t");
for (Var i=0;i<this.person.length;i++) { prints out the entire contents of the I element in the person array sequentially
Console.log ((i+1) + "\ T" +this.person[i].name+ "\ T" +this.person[i].meth+ "\ T" +this.person[i].chinese+ "\ T" + this.person[i].english+ "\ t" +this.person[i].sum);


Console.log ("Jerry's educational Performance Management System");

While (true) {
Console.log ("1. Student score Entry 2. Show all student results");
var num = prompt ("Please select operation sequence Number");
switch (num) { determines which function to call by using switch to determine the input option
Case "1":
Classes.addperson ();
Break ;
Case "2":
Classes.showperson ();
Break ;
Console.log ("Whether or not to continue (y/n)");
var isgo=prompt ("Y continue, other characters cancel");
if (isgo== "Y" | | isgo== "Y") { determines whether to end a while loop by using the IF option to determine the input
Break ;

Console.log ("Exit the system, thank you for using");

Example two: application of Yang Hui triangle

[1,3,3,1], i=4-1 j0=1+j1=3
[1,4,6,4,1], 4:i=4 j1=4

As shown first, the Yang Hui Triangle is written as right triangle, and then it becomes the structure of a two-dimensional array.

/*************** two-dimensional array structure *************************/
var num =parseint (Prompt ("Please enter the number of Yang Hui triangle Rows"));
var arr = new Array (num); create a new array with num elements arr
for (Var i=0;i<num;i++) {
Arr[i]=new Array (i+1); The basic two-dimensional array framework is obtained by looping to define the first element of ARR as an array of i+1 elements, respectively.

/****************** fill in the data **************************/

for (Var i=0;i<arr.length;i++) {A two-layer loop nesting is required to put data into a two-dimensional array, the first loop is the number of outer arrays, and the second loop is the number of inner arrays

                  Use Arr[i].length to express
for (Var j=0;j<arr[i].length;j++) {
if (j==0| | J==i) { The law of the Yang Hui Triangle: the first and last are 1, i.e. arr[i][0]==arr[i][i]==1, where the if is used to determine whether ARR[I][J] is 1
}else{ when not arr[i] the first and last time of the law: each number is the same position in the previous row and the number of the previous position of the sum, example arr[4][1]=arr[3][1]+arr[3][0];
ARR[I][J]=ARR[I-1][J]+ARR[I-1][J-1]; that is, execute this calculation
Console.log (Arr[i].tostring ()); output each arr[i as a string]

Two: Regular expressions

1. Declarations of regular Expressions:

① literal declaration: var reg =/Expression rule/expression pattern

② using the New keyword declaration: var reg = new RegExp ("Expression rule", "expression pattern")

2. Common methods of regular expressions:
The. Test (): method is used to detect whether a string matches a regular pattern. Returns TRUE or FALSE.

3. Common patterns of regular expressions:

G: Global match without G, default non-global match, match only the first qualifying string.

I: Ignore case, do not add default to match case.

M: Matches multiline pattern (string is displayed with multiple lines, each line has a beginning end).

Note: To determine whether input in the form form satisfies the requirements, add ^ and $ at the beginning of the regular expression, or the rule will not be correctly judged.

Example: Determining whether a mailbox conforms to a rule

var regm =/^\[email protected]\w+\. [A-za-z] {2,3} (\. [A-za-z] {2,3})? $/;

 Common web site for judging regular expressions: https://regexper.com/

Three: JQuery

1.JQuery syntax

①. Through jquery ("selector"). Action (); Invoke the event function through the selector, but in jquery, jquery can be replaced by $ ("selector"). Action ();

Selectors can use CSS selectors to select elements.

The. Action () represents the action performed on the element.

②. Document-Ready function: Prevents the document from running jquery code before it is fully loaded.

$ (document). Ready (function () {
jquery Code

Shorthand method
$ (function () {
jquery Code

The document-ready function needs to be executed only after the Web page DOM structure has been loaded. Do not need pictures, videos and other full loading complete, save time.

The document-ready function can write multiple and not be overwritten.

The ③.jquery object goes with the native DOM object:
Native object to jquery object: $ (DOM object);
var p=document.getelementsbytagname ("P");
$ (P);//Convert to jquery object

The jquery object goes to the native DOM object:
$ ("#p"). Get (0); $ ("#p") [0];
Example: $ ("#p"). Get (0). Style.color = "Red";

2. Event Delegation:

①: Using on for event delegation
>>> Event Delegation: Binds an event that would otherwise be bound to an element to its parent element
or root node, and then delegate to the element to make the event effective on that element.

Example: $ ("P"). On (' click ', Function () {});

$ (document). On ("click", "P", function () {});

>>> function: The default binding method can only be bound to the P element that is already present at the beginning of the page
Cannot bind to the new P when the page is added to the P element.
With event delegation, new elements can also bind on events.


JSON object

The ①j object is a collection of key-value pairs, separated by ":" Between the keys and values, separated by "," between multiple key-value pairs
② multiple JSON objects can be placed in an array. JSON objects and arrays can be nested with each other.

The key of the ③json object must be a string.

The way to get the JSON $.post ("URL of json", function (data) {});

JS custom objects, regular expressions, some of the knowledge points in jquery

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.