Home >Web Front-end >JS Tutorial >JavaScript Distilled basic knowledge and functions_javascript skills

JavaScript Distilled basic knowledge and functions_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:30:271075browse

[ ] () Attribute access and function call delete new typeof - ! Unary operator
* / % Multiplication, division, modulo
- Addition/connection, subtraction

>= <= > < Inequality operator === !== Equality operator && Logical AND || Logical Or ?: Ternary operator

2. Statements:

1.if statement




Copy code

The code is as follows:

var dayOfWeek=0; if(day===6){
}
else if(dayOfWeek===0){
} else{ } 2.switch statement




Copy code


The code is as follows:

var dayOfWeek=0; switch (dayOfWeek){
case 6:
break;
case 0: break; default: break; }
The switch statement exactly matches the value of the switch expression with all specified Case expressions (===). When a match is found, the statements in the matching case clause are executed. If no match is found, the optional default statement is executed.

3. while statement





Copy code


The code is as follows:


var count=0; while (count<=10){ count ;
}

Copy code


The code is as follows:

var count=0; do{ count;
}while (count<=10);

Copy code


The code is as follows:

for (var count=0;count<=10;count ){ }
6. for/in statement
Copy code


The code is as follows:

var colors= ["Red","Yellow","Blue"]; for (var color in colors){
}
Copy code


The code is as follows:

try{ } catch(e ){
}

Copy code


The code is as follows:

throw{ name:'ArgumentOutOfRangeError', message:'year must > 0'
}

9.return statement
return "Red";
return statement will cause a function to return early. It can also specify the value to be returned. If no return expression is specified, its value is undefined.

3. Type:
1. Number
var value=1.0;
JavaScript has only a single number type. It is represented internally as a 64-bit floating point number.
Special numerical values:
NaN non-numeric special value
Infinity special value of infinity
2. String
var value="One";
JavaScript string is a string consisting of 16 A sequence of Unicode characters. String literals can be enclosed in single or double quotes.
String escape:
" ' \
b Backspace character
f Form feed character
n Line feed character
r Carriage return character
t Tab character
uXXXX Unicode character specified by 4-digit hexadecimal XXXX
3. Boolean value
var value=true;
Boolean value has only 2 values: true and false.
The following values ​​are considered false. Values:
false
null
undefined
Empty string''
Number 0
Number NaN
4.null
var value=null; //null
null is a special value in JavaScript, which means "no value".
5.undefined
var value; //undefined
undefined is a special value in JavaScript. Variables, or variables that have been declared but have not been assigned a value, and object properties that do not exist will return undefined. Object

var car={
brand:"Honda",
color:"Red",
run:function(){
//run
}
};
var value=car.brand;


Objects in JavaScript are variable key-value collections. Container, where each attribute has a name and a value. The attribute name can be any string, including the empty string, and the attribute value can be any value except the undefined value.

Copy code
The code is as follows: car.prototype.stop=function(){ //stop };
car.run();


Every object is connected to a prototype object, and it can inherit properties from it. Prototype connection has no effect when updating.
Modularization:



Copy code
The code is as follows: var App={}; App.employee={ name:"Joyce"
}
App.customer={
name:"Jason"
}


7. Function



Copy code
The code is as follows: var add= function(a,b){ return a b; };
add(1 2);


Functions in JavaScript are objects. Functions can be defined within other functions. An inner function can access the parameters and variables of the outer function that surrounds it. This is called a closure.
Closure:



Copy code
The code is as follows: var car=function(brand ,color){ return { getBrand:function(){
return brand;
},
getColor:function(){
return color;
}
};
};
var myCar=car("Honda","Red");
myCar.getBrand();



Functional inheritance:



Copy code
The code is as follows: var mammal = function (spec) { var that = {}; that.getName = function () {
return spec.name;
};
that.says = function () {
return spec.saying || '';
};
return that;
};
var myMammal = mammal({name: 'Herb'});
var cat = function (spec) {
spec.saying = spec.saying || 'meow';
var that = mammal(spec);
that.purr = function (n) {
//purr
};
that.getName = function () {
return that.says() ' ' spec.name ' ' that.says();
};
return that;
};
var myCat = cat({name: 'Henrietta'});


8. Array
var colors=["Red","Yellow","Blue"];
var value=colors[0]; //"Red"
Arrays, like objects, are key-value collections. The difference is that arrays can use integers as attribute names. Arrays also provide a very useful set of built-in methods.
Every array has a length property. The value of the length attribute is the largest integer attribute name of this array plus 1. It is not necessarily equal to the number of attributes in the array.
9. Regular expression
Copy code The code is as follows:

var numberRegex=/ ^-?d (?:.d*)?(?:e[ -]?d )?$/i;
numberRegex.test(1.2); //true

Regular Expression grouping:
() Capturing grouping
(?:) Non-capturing grouping
Regular expression escape:
\ / [ ] ( ) { } ? * | . ^ $
f form feed character
n line feed character
r carriage return character
t tab character
uXXXX Unicode character specified by 4-digit hexadecimal XXXX
d matches a number (equivalent to in [0-9])
1 Reference to capture group 1 (2 and so on)
Regular expression class escape:
- \ / [ ] ^
b Backspace character
f form feed character
n line feed character
r carriage return character
t tab character
uXXXX Unicode character specified by 4-digit hexadecimal XXXX
d matches a number (equivalent to in [0-9])
Regular expression quantifier:
? Match 0 or 1 times (same as {0,1})
* Match 0 or more times (same as {0,})
Match 1 or more times (same as {1,})
{n} Match n times
{n,} Match at least n times
{n,m} Match at least n times, but not more than m Times
regular expression flags:
g performs global matching (all matches)
i performs case-insensitive matching
m performs multi-line matching (^ and $ can match line terminators)
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:When getting focus, use js timer to set time to execute action_javascript skillsNext article:When getting focus, use js timer to set time to execute action_javascript skills

Related articles

See more