JavaScript syntax study notes

A variable

was star = "blog";
There num = 1.1;

 

Second, the calculation

+, -, *, /: Math

%: Remainder

Operators of different priorities, preferably longer operational expression using brackets.

 

Third, determine the

if

var score=70;
if(score>=60)
    alert ( "pass"); // Score> = 60 
the else 
    alert ( "fail"); // Score <60

var Math = 90 ;
 var Dictionary Dictionary English = 60 ;
 IF (Math> 80) // the else nearest match IF 
    IF (Dictionary Dictionary English> 60 )
        alert ( "Excellent"); // Math> 80 && Dictionary Dictionary English> 60 
    the else 
        alert ( "good"); // Math> 80 && Dictionary Dictionary English <= 60 
the else 
    alert ( "difference"); // Math <80

IF (Math> 80) // can be used to distinguish between complex {} is determined priority 
    IF (Dictionary Dictionary English> 60 ) {
        alert ( "Excellent"); // Math> 80 && Dictionary Dictionary English> 60 
    }
     the else 
        alert ( "good"); // Math <80

 

switch

switch(color)
{
case “red”:
    alert ( "stop"); // Color = "Red", the output stop 
    BREAK ;
 Case "Yellow":
    alert ( "ready"); // first output ready, and then output the line, there is no BREAK 
Case "Green":
    alert ( "OK"); // output lines 
    BREAK ;
 default :
    alert ( "bad light"); // in addition to the "red", "yellow", "green" Other possible 
    BREAK ;
}

 

Fourth, the cycle

while

var count=10;
while(count>0){
    console.log(count);
    count--;
} // not output 0

 

do-while

do{

}while(condition);

var count=10;
do{
    console.log(count);
    count--;
} The while (COUNT> 0); // outputs 0

 

for loop

for(init;condition;step){

}

for(var count=10;count>0;count--){
    console.log(count);
}

 

for loop through the object element

 

for(var x in object){
    console.log(object[x]);
}

 

V. function

Statement function in two ways

function fun_name(){}
function fun_name(parameter){}
var f = new Function("x","y","return x*y");
//等价于
function f(x,y){return x*y;}

JavaScript function may be transmitted as the variable parameter, which is somewhat similar to the C function pointers.

Sixth, the array

Several ways to declare an array of

var arr1=new Array();
var arr2=new Array(size);
var arr3=new Array(d1,d2,dn);
var arr4=[d1,d2,dn];

JavaScript array index is counted from 0, arr [0] is the first element.

Gets an array of length

arr.length; // length is the number of elements within the array

The array may be cut by modifying the length of the array.

Output array

var arr=new Array(1,2,3);
console.log(arr);//[1,2,3]
console.log(arr.toString());//1,2,3
console.log(arr.valueOf());//[1,2,3]
console.log(arr.join(";"));//1;2;3

Stack operations

var arr=new Array();
var count=arr.push("data1");
console.log(count);//1
count=arr.push("data2");
console.log(count);//2
var item=arr.pop();
console.log(item);//data2
console.log(arr.length);//1

Queue operation

var arr=new Array();
var count=arr.push("data1");
console.log(count);//1
count=arr.push("data2");
console.log(count);//2
var item=arr.shift();
console.log(item);//data1
console.log(arr.length);//1

Sorting operation

was arr = [7,5,3,9,5,1 ];
arr.sort();
console.log(arr);//1,3,5,5,7,9
arr=[7,5,3,9,5,1];
arr.reverse();
console.log(arr);//1,5,9,3,5,7

You can write your own comparison function to sort function

function mycmp(value1,value2){
    if(value1<value2){
        return 1;
    }else if(value1>value2){
        return -1;
    }else {
        return 0;
    }
}

was arr = [7,5,3,9,5,1 ];
arr.sort(mycmp);
console.log(arr);//[9, 7, 5, 5, 3, 1]

Array Operations

where , R1 = [1,2,3,4,5 ];

arr2 is = arr1.concat (. 6, [7, 8]); // connect 
the console.log (arr2 is); // [. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8] 

ARR3 = arr1.slice ( 1,4); // intercept 
the console.log (ARR3); // [2,. 3,. 4]


// splice (start position, the number of delete, insert elements) 
// splice start start position to delete the specified number of the element, and then insert elements from the specified location 
arr1.splice (0,2 );
console.log(arr1);//[3, 4, 5]
arr1.splice(2,0,6,7);
console.log(arr1);//[3, 4, 6, 7, 5]
arr1.splice(2,1,8,9);
console.log(arr1);//[3, 4, 8, 9, 7, 5]

 

Seven, Object

Create Object

var obj=new Object();
var rect={length:5,width:4};

Access object properties

var hero=new Object();
hero.name = "Shangxiang" ;
hero.ablity=new Object();

Even when there is no configuration attributes can also be increased at any time in the future

Delete object properties

delete rect.length;
rect.length=null;

Construction method

1 does not directly producing the object

2. defined by this member

3. There is no return

function circle(r){
    this.r=r;
    this.area=function(){return r*r*3.14;};
}

var c=new circle(2);
console.log(c.area());

By constructing method, we can become a target, "a class of objects", do not repeat repeated assignment for a new kind of object.

Prototype object

Similar objects have in common defaults, the next figure Person1 and Person2 just initialize itself no other properties, will visit the property Prototype Person when accessing its properties. After assignment to a property Person1, Person1 have their own name, name at this time access is Person1 own.

 

 Set Prototype

obj.prototype.property=data;
obj.prototype={
    property1=data1;
    property2=data2;
}

Prototype and construction methods can be used in conjunction with Prototype can share data, independent data created by this constructor.

Guess you like

Origin www.cnblogs.com/guoyicai/p/11787577.html