JavaScript Basics (vii) Arrow functions generator Date JSON

Source: Internet
Author: User

The ES6 standard adds a new function: arrow function.
x = x *x
The arrow above is equivalent to:
function (x) {
return x*x;
}

The arrow function is equivalent to an anonymous function and simplifies the function definition. A type like the one above that contains only an expression,
Even {...} and return are omitted. There is one more statement that can contain multiple statements, and you cannot omit the {...} and return:
X =>{
if (x > 0) {
return x * x;
}else{
Return-x *x;
}
}

If the argument is not one, it needs to be enclosed in parentheses ():
Two parameters
(x, y) = x*x + y *y
no parameters;
() =>3.14
Variable parameters
(x, y,... rest) =>{
var i, sum = x +y;
for (i=0;i<rest.length;i++) {
Sum + = Rest[i];
}
return sum;
}

This
Now, the arrow function completely fixes the point of this, which always points to the lexical scope, which is the outer caller obj:
var obj = {
birth:1990,
Getage:function () {
var B = This.birth; 1990
var fn = () = new Date (). getFullYear ()-This.birth; This points to the Obj object.
return FN ();
}
}
Obj.getage (); 25

If you use the arrow function, the former kind of hack notation;
var = this;

It's no longer necessary.
Since this is bound in the arrow function according to whether the scope is scoped, it is not possible to invoke the arrow function with call () or apply ().
binding, that is, the first parameter passed in is ignored.
var obj = {
birth:1990,
Getage:function (year) {
var B = This.burth; 1990
var fn = (y) =>y-this.birth; This.birth is still 1990.
Return Fn.call ({birth:2000},year);
}
};
Obj.getage (2015); 25

Generator
Generator (generator) is a new type of data introduced by the ES6 standard. A generator looks like a function, but can be returned multiple times.

function* foo (x) {
Yield x +1;
YIELDX + 2;
return x +3;
}

Generator and functions differ, generator is defined by the function* (note the extra * number) and, in addition to the return statement, can be returned multiple times with yield.

function can only be returned once, so Tourmaline returns an array. However, if you switch to generator, you can return one number at a time, returning multiple times.
function* fib (max) {
var T,
A = 0,
B=1,
N=1;
while (n < max) {
Yield A;
t = a +b;
A = b;
b = t;
n++;
}
return A;
}

Direct call Try:
FIB (5); fib {[[Generatorstatus]]: "Suspended", [[[Generatorreceiver]]: window}

calling a generator directly is not the same as calling a function, and fib (5) simply creates a generator object and does not execute it.    The
calls the generator object with two methods, one to keep calling the next () method of the Generator object:
var f = fib (5);
F.next ();    {value:0, done:false}
F.next ();//{value:1, done:false}
F.next ();//{value:1, done:false}
F.next (); {value:2, done:false}
F.next ();//{value:3, done:true}

Date
in JavaScript, date objects are used to represent dates and times.    
to get the current time of the system, using:
var now = new Date ();
Now Wed June 19:49:22 gmt+0800 (CST)
Now.getfullyear ();//2015, Year
Now.getmonth ();//5, Month, note month range is 0~1    1,5 = June
Now.getdate ();//24, representing 24th #
Now.gethours ();//3, which means Wednesday
now.getminutes ();//19, 24-hour system
Now.getseconds (); 22, Seconds
Now.getmilliseconds (),//875 milliseconds
Now.gettime ();//1435146562875, timestamp in number form
If you want to create an execution date and    Date object of the time, which can be used:
var d = new Date (2015,5,19,20,15,30,123);
d;//Fri June 20:15:30 gmt+0800 (CST)

Json
JSON is the abbreviation for JavaScript Object notation, which is a data interchange format.
In JSON, there are a total of several data types:
1,number: Exactly the same as the number of JavaScript;
2,boolean: Is the true or false JavaScript;
3,string: is the JavaScript String;
4,null: Is the null of JavaScript;
5,array: Is the array representation of JavaScript--[];
6,object: This is the way JavaScript {...} is represented.

Son's dead. The character set must be UTF-8, which means that there is no problem with multiple languages. For unified parsing, the JSON string must be in double quotation marks "", and the key of object must also be double-quoted "".

Serialization of
var Guagua = {
Name: ' Xiaoming ',
Age:14,
Gender:true,
height:1.65,
Grade:null,
' Middle-school ': ' \ ' w3c\ ' Middle school ',
Skills: [' JavaScript ', ' Java ', ' Python ', ' Lisp ']
};
Json.stringify (xiaoming); ' {' name ': ' Xiaoming ', ' age ': +, ' gender ': true, ' height ': 1.65, ' grade ': null, '

Middle-school ":" \ "w3c\" Middle School "," Skills ": [" JavaScript "," Java "," Python "," Lisp "]} '
Results:
{
"Name": "Xiaoming",
"Age": 14,
"Gender": true,
"Height": 1.65,
"Grade": null,
"Middle-school": "\" w3c\ "Middle School",
"Skills": [
"JavaScript",
"Java",
"Python",
"Lisp"
]
}

The second parameter controls how the object's key value is filtered, and if we only want to output the specified property, we can pass in the array:
Json.stringify (xiaoming, [' Name ', ' skills '], ');
Results:
{
"Name": "Guagua",
"Skills": [
"JavaScript",
"Java",
"Python",
"Lisp"
]
}

You can also pass in a function so that each key-value pair of an object is processed first by the function:
function convert (key, value) {
if (typeof value = = = ' String ') {
return Value.touppercase ();
}
return value;
}
Json.stringify (Guagua, convert, ');

The above code capitalizes all attribute values:
{
"Name": "Guagua",
"Age": 14,
"Gender": true,
"Height": 1.65,
"Grade": null,
"Middle-school": "\" w3c\ "Middle School",
"Skills": [
"JAVASCRIPT",
"JAVA",
"PYTHON",
"LISP"
]
}

If we also want to control exactly how to serialize xiaoming, you can define a Tojson () method for Xiaoming to return directly the data that the JSON should serialize:

Deserialization
To get a string in JSON format, we use Json.parse () to turn it into a JavaScript object:
Json.parse (' [1,2,3,true] '); [1,2,3,true]
Json.parse (' {' name ': ' Melon ', ' age ': 14} '); Object{name: ' Melon ', age:14}
Json.parse (' true '); True
Json.parse (' 123.45 ')://123.45

Json.parse () can also receive a function to convert the parsed properties:
Json.parse (' {' "name": "Guagua", "Age": +} ', function (key,value) {
Put number * 2
if (key = = = ' name ') {
return value + ' classmate '
}
return value;
}) ; Object{name: ' Melon classmate ', age:14}

JavaScript Basics (vii) Arrow functions generator Date JSON

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.