A brief analysis of JavaScript arrow function generator Date json_ Basics

Source: Internet
Author: User
Tags character set current time generator json javascript array

A new function is added to the ES6 standard: arrow function (arrowhead).

x => x *x
the arrow above is equivalent to:
function (x) {return 
x*x;
}

The arrow function acts as an anonymous function and simplifies the definition of the function. A kind of like above, contains only one expression,

Even {...} and return are omitted. There is also a can contain more than one statement, this time you cannot omit {...} 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,... Res        T) =>{ 
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 to 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 previous type of hack;

var that = this;

Will not be needed anymore.

Because this is already bound to the scope in the arrow function, it is not possible to use call () or apply () to invoke the arrow function.

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;
}

The difference between generator and functions is that the generator is defined by the function* (note the extra * number) and can be returned multiple times with yield in addition to the return statement.

The function can only be returned once, so Tourmaline returns an array. However, if you change to generator, you can return one number at a time, and return 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; 
}

Try the direct call:

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

Directly calling a generator and calling a function is not the same, FIB (5) simply creates a generator object that has not yet been executed.

There are two ways to invoke the generator object, one is to continuously invoke 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, a Date object is used to represent dates and times.

To get the system current time, use:

var now = new Date ();
Now Wed June 2015 19:49:22 gmt+0800 (CST)
now.getfullyear ()//2015, Year
now.getmonth ();//5, Month, note month range 0 ~11,5 said June now.getdate ();//
24, indicating 24th
now.gethours ();//3, indicating Wednesday
now.getminutes ();//19, 24-hour system 
    now.getseconds (); 22, Sec
now.getmilliseconds ();//875 millisecond
now.gettime ();//1435146562875, timestamp in number form
if you want to create a hold    The date object for the row dates and times, which can be used:
var d = new Date (2015,5,19,20,15,30,123);
d;//Fri June 2015 20:15:30 gmt+0800 (CST)

Json

JSON is the acronym for JavaScript Object notation, which is a data interchange format.

In JSON, there are just a few types of data:

1,number: Exactly the same as the number of JavaScript;

2,boolean: Is the JavaScript true or false;

3,string: is the JavaScript String;

4,null: is the JavaScript null;

5,array: Is the JavaScript array representation--[];

6,object: Is the javascript {...} notation.

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

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": "
Gender": true,
"height": 1.65, "
grade": null,
" Middle-school ":" \ "w3c\" Middle School ",
" skills ": [
JavaScript, Java,
Python,"
Lis P "
]
}

The second parameter controls how the key value of the object 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 pair of the object is processed by the function first:

function convert (key, value) {
if (typeof value = = ' String ') {return
value.touppercase ();
}
return value;
}
Json.stringify (Guagua, convert, ');

The code above capitalizes all the attribute values:

{
"name": "Guagua", "Age
": "
Gender": true,
"height": 1.65, "
grade": null,
"M Iddle-school ":" \ "w3c\" Middle School ",
" skills ": [
JAVASCRIPT, JAVA,
PYTHON,"
LI SP "
]
}

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

Deserialization

To get a JSON-formatted string, 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 ': ') '//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 property:
json.parse (' {' name ': ' Guagua '), ' Age ': ', function (key,value) { 
//number * 2
if (key = = = ' name ') {return 
value + ' classmate '
}
value;
}) ; Object{name: ' The melon classmate ', age:14}

The above analysis of JavaScript arrow function Generator Date JSON is a small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.