Data type
There are two types of JavaScript data: Basic type and Object type
Basic type:
Number, String, Boolean, null, undefined
where null and undefined represent unique members of their particular type
Object type:
The so-called object type is a collection of attributes, each of which consists of key-value pairs
Normal objects are unordered key-value pairs, and ordered key-value pairs are our arrays.
And a special object is a function.
A function is an object with executable code associated with him, running executable code and returning results by calling a function
If you create a new object using the new operator, we call it a constructor.
Each constructor defines a class-a collection of object combinations initialized by constructors (which can be considered subtypes of object types)
Array is a class (array)
Functions are classes (function)
Date is class (date)
Regular is the class (REGEXP)
Class and function through JS always
Digital
JavaScript does not differentiate between integers and floating-point numbers, and all numbers are represented by floating-point numbers
Math
Now let's get to know an object that has a lot to do with numbers. Math, this guy can do many things!
Math.Round (0.6) rounding
Math.random () generates a pseudo-random number greater than 0 and less than 1
Math.ceil (0.6) method to round a number
Math.floor (0.6) method to round a number
Operation Overflow
When the JS operation overflow will not error, will use infinite large infinity and infinitesimal-infinity expression;
If there is no meaningful operation to return to Nan,nan's judgment anyway is false (he is not equal to himself), so judge him to use the isNaN () function
Parseint and Parsefloat
parseint (string, radix) radix, which represents the cardinality of the conversion, which is what we often call 2-binary, 8-binary, 10-binary, 16-binary, and so on. range from 2~36, but we generally call this method in JS, the basic is the base of 10 to convert. If the parameter is less than 2 or greater than 36, parseint () returns NaN.
parseint This function is very useful, you can return a string as an integer
<script type= "Text/javascript" > var a = parseint (' 334DD '); var B = parseint (' 3D34DD '); var c = parseint (' SS3D34DD '); s = ';</script>
String
A string is an immutable ordered sequence of 16-bit values, each of which is usually derived from the Unicode character set.
String once the first is not changed, we use the function operation he just returned the new string, the following describes the string-related functions
vars = ' Hellow world '; S.charat (0);//hS.charat (s.length-1);//DS.substring (1, 4); Ell 2nd to Fourth character S.slice (1, 4);//Ibid .S.slice (-3);//Rld, last three charactersS.indexof (' l ');//2 first occurrence of L positionS.lastindexof (' l ');//the last occurrence of L position,S.indexof (' l ', 3);//3 position of L first appeared after position 3S.split (', ');//using "," split the string into an array, very useful guyS.replace (' h ', ' H ');//Full text substitution, support for regular expressionsS.touppercase ();//Convert to uppercaseBoolean value
Boolean value only true with false two
null and undefined
Null in JS is used to describe "null", using typeof null to return an object, which means null is a special object, but the object is only one person;
Undefined represents an empty value, indicating that the variable is not initialized, such as
var A, in this case a is undefined, if we define an object, but he starts with no value can set it to null.
Wrapping Object
We see in front of S as a string, but can execute a lot of functions, this is simply the behavior of the object, because he really became an object ...
JavaScript converts a string to an object through new string (s), so S has a method, and once the function operation ends the newly created object is destroyed.
The number is the same as the bool value of the wrapper.
Type conversions
The type of JavaScript is very flexible, and the type conversions sometimes happen automatically:
+ ' SSS '//=>-SSS ' 7 ' * ' 4 '/281-' d '//nan1-' d ' + ' s '//nans (note)
Object literal
Object literals are really good things in JS, object literals provide a convenient representation for creating new object values:
var obj = {' name ': ' Johnny ', Age: ' Male ', sayname:function () {alert (this.name);}};
We can create objects in the form of new object, but this is still recommended.
We access the object by means of. or []:
obj.nameobj[' name ')if you want to determine if an object contains a property, use: Obj.hasownproperty (' age ')
Serialized Object (Json)
If we serialize the object literal, he becomes a generic string, and we affectionately call him a JSON string!
The JSON string is a magical guy who can convert two-dimensional data from a data table into a string that, when passed to the front end, will revert back to two dimensions,
Array
The array in JS is incredibly flexible!!! Because he can put things at random, and his length will be very flexible.
Initialization
var arr = [];var arr = new Array ();
Length
The length of the array is a magical stuff:
<script type= "Text/javascript" > [].length;//0 [' A ', 1, function () {}].length;//3 var arr = [1, 2, 3, 4, 5]; 5 elements arr.length = 3;//arr = [1, 2, 3] arr.length = 0;//arr = [] arr.length = 6;//equal to Arr = new Array (6) < ;/script>
Adding array items dynamically
var arr = [];arr[arr.length] = 1;...arr.push (1);
Array traversal these will not say ... Let's take a look at the array functions
Join
This function corresponds to the spilt of a string, which converts a number to a string
var arr = [1, 2, 3];arr.join ();//' Arr.join ' (';'); /' 1;2;3 '
Reverse
Used to flip an array
Sort
This guy we have to remember him because he's very common:
<script type= "Text/javascript" > var arr = [1, 4, 562, A, 4346, 433, 645]; Arr.sort (); var ss = Arr.join (); var s = ";</script>"
Of course we're going to have more complicated situations, like we're going to sort of age, and our arrays are not that single:
<script type= "Text/javascript" >
Arr.sort (function (A, b) {return a.age-b.age;}); </script>
Concat ()
Create and return a new array:
var a = [1, 2];a.concat (3, 4);//1,2,3,4a.concat ([2], [3, 3]); 1,2,2,3,3a.concat (4, [5, [6, 7]]);//1,2,3,4,5,[6,7]
Slice ()
Returns a fragment of the specified array, returning a new array containing elements from start to end (excluding the element) from the Arrayobject:
var arr = [1, 2, 3, 4, 5];a.slice (0, 3);//1,2,3a.slice (3);//4,5a.slice (1,-1);//2,3,4a.slice (-3,-2);//3// A negative number is the position relative to the last element
Variables and scope variable declarations
JS uses Var to declare variables:
var a = 1;
var a = 1,
b = 2;
Duplicate declaration
It is harmless to declare a variable repeatedly, and if it is repeated and assigned, it is the same as a simple assignment statement:
<script type= "Text/javascript" > var s; var s = ' a '; var s = {1: '}; The above statement is equivalent to var s; s = ' a '; s = {1: '};</script>
Variable scope
Scope is the area in the program source code that defines the variable.
A variable within a function has the property of a local variable and is not accessed by an external variable.
Declaration in advance
There is no block-level scope ({}) in JavaScript, JavaScript has a function scope, variables defined anywhere in the function are meaningful, and an interesting thing is that the variables I define later can actually be used in the first place:
<script type= "Text/javascript" > var a = ' outer '; (function () { alert (a);//undefind var a = ' inner '; alert (a);//inner }) ();</script>
Perhaps some friends are puzzled about this, in fact he is the meaning of:
<script type= "Text/javascript" >// var a = ' outer '; (function () { alert (a);//undefind var a = ' inner '; alert (a); Inner //code equivalent to var A; alert (a); Undefind a = ' inner '; alert (a); Inner }) ();</script>
operator | |
Or for the IF condition to judge there is nothing to say, but we may have different usages in the assignment operation:
function (a) {a = a | | {};} If a does not exist, initialize a As object to avoid error.
This usage is very useful.
In operator
In is used to determine whether a property is in an object:
var obj = {x:1, y:2}; ' X ' in Obj;//true ' Z ' in Obj;//falsefor (var k in obj) {//Traversal object}
instanceof operator
This operator is used to determine whether an object belongs to a class:
var d = new Date ();d instanceof date; trued instanceof object;//trued instanceof Number;//false
Eval operator
Eval This guy is very powerful!!! He is equivalent to a JS compiler, you can use the string as JS execution, of course, security efficiency will have some problems.
Eval (' 2+3 ');//5function A () {alert (' AA ');} Eval (' A (); ');
typeof operator
typeof is used to determine the type of an object:
typeof undefined; Undefindtypeof null; Objecttypeof true; Booleantypeof 1; or nan;numbertypeof ' SSS '; stringtypeof function () {}; function
The basic language of "JavaScript learning"