Typejavascript notes----strings, arguments, functions, expressions, Loops

Source: Internet
Author: User

A new character of string--double 丿 (apostrophe) declaration string
, multi-line string
var myName = ' Fang
My Hao Li
Jsk c SDF '//this will not Error.
, String templates
Console.log (' Qsdaf ${myname} ')
Console.log (' Qsdaf ${myname ()} ')//functions and variables can be written directly inside the ${}
Console.log (' <div>
<span>${myName}</span>
</div> ')//so that it can be written quickly, without a plus connection;
, auto-split character string
Function Test (a,b,c) {
Console.log (a);
Console.log (b);
Console.log (c);
}
var myname = ' Ni hao ';
var getage = function () {
return 18
}//the following call, A is an array [' hello my name is ', ', im ', ']; B is ' Ni hao '; c is 18; that is, it is separated by a variable
Test ' Hello my name is ${myname},im ${getage ()} ';
Ii. New characteristics of parameters
1. parameter type--use a colon after the parameter name to specify the type of the parameter
, any can be of any type
var myname:any = 18;
, string String type
var myname:string = "zhang san"
, number numeric type
var my:number = 20
, Boolean Boolean type
var Name:boolean =true
Function Test (name:string): String {
Return ""
}//means that a parameter can only be a string, and the return value of a function can only be a string;
, Custom types
Class Person {
name:string;
age:number;
}
var Zhangsan:P erson = new Person ()
Zhangsan.name = "zhailiang";
Zhangsan.age = 18
2. parameter default value--specify The default value of the parameter with an equal sign after the parameter declaration
var my:string = "fang";
Function Test (a:string,b:string,c:string= "wo") {
Console.log (a)
Console.log (b)
Console.log (c)
}
test{' SFA ', ' sss ', ' ASD '}//do not give default values must give 3 parameters otherwise will error; parameters with default values can not be written (but must be written on the last Side)
3, optional Parameters--after The parameter declaration of the method with a question mark to indicate that this parameter is an optional parameter
Function Test (a:string,b?:string,c:string= "wo") {
Console.log (a);
Console.log (b);//error when B.length
Console.log (c);
}
test{' SFA '}//this will not error, B is undefined; and a note. as with the default parameters, optional parameters must be written after the required write parameter
Iii. new features of functions
1. Rest and spread operator (...) )--used to declare any amount of method parameters
function Func1 (.... Args) {
Args.foreach (function (arg) {
Console.log (arg);
})
}
Func1 (func1) (1,3,4,5,6)//forward Use
function Func2 (a,b,c) {
Console.log (a);
Console.log (b);
Console.log (c);
}
var args = [1,2],ARGS1 = [5,6,7];
Func1 (... args); func1 (... args1)//reverse
2. Generater function--control function Execution process, manual pause and Recovery code execution
function* dosomething () {
Console.log ("start");
Yield
Console.log ("finish")
}
var fun1 =dosomething ();
Func1.next ();//will stop in the yield place
Func1.next ();
3. Destructuring destructor expression--to disassemble an object or an array into any number of variables through an expression
function Getstock () {
return {
Code: "IBM",
price: {
price1:100,
price2:200
}
}
}
var {code:codex,price:{price2}} = Getstock ();
Console.log (codex);
Console.log (price2)//disassemble from Object
var array = [1,2,3,4];
var[number1,number2] = array;
Console.log (NUMBER1)//1
Console.log (NUMBER2)//2
var[,,, number1,number2] = array;
Console.log (NUMBER1)//3
Console.log (NUMBER2)//4
var[number1,number2,... others] = array;
Console.log (NUMBER1)//1
Console.log (NUMBER2)//2
Console.log (others)//[3,4]
Iv. Expressions and Loops
1. Arrow expression--used to declare anonymous function, eliminate this pointer problem of traditional anonymous function
var sun = (arg1,arg2) =>arg1+arg2//only One line does not have to write Curly braces, also does not have to write the return keyword
var Sun =arg3 =>console.log (arg3)//do not write parentheses when there is only one parameter
var MyArray = [1,2,3,4,5,6];
Console.log (myarray.filter (value =>value%2 = = 0))

function Gerstock (name:string) {
THIS.name = name;
SetInterval (() = {
Console.log ("name is:" + this.name);
},1000)
}
var stock = new Getstock ("IBM")//can be seen typed as "name is:ibm", eliminating the traditional anonymous function this pointer problem, if you write with ES5 can not get IBM
2, ForEach (), for in and for
var MyArray = [1,2,3,4];
Myarray.desc = ' Four number ';
Myarray.foreach (value=>console.log (value))//ignores Its properties and cannot be interrupted
For (var N in MyArray) {
Console.log (n)//key and attribute of key-value pairs typed 0,1,2,3,desc
Console.log (myarray[n])//each Value played
}
For (var N of MyArray) {
If (n>2) break;
Console.log (n)
}//and foreach can just be interrupted by a break, a collection, an array can be used, or it can be used on a string, which will print every character of the String.

Typejavascript notes----strings, arguments, functions, expressions, Loops

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.