Today I'm going to summarize some of the things ES6 does with functions. Combined with my ES5 function features, can let us according to the actual situation, to very good completion of our project requirements. One, ES6 arrow function
1. Arrow
Document. onclick = function () {// This of a common function is temporarily bound during running. That is to say, the function is not running, you absolutely cannot know who this is. // if this function is called by yourself, this is a window. For
ES6 can use the arrow to define a function, note that it is a function, and do not define the class (constructor) in this way.First, the grammar1. Simple function with one parametervar single = a + asingle (' Hello, World ')//' Hello, World ' 2.
No parameter amount arrow functionIf the function does not have arguments, we can say by (), is not some force lattice, see exampleConst FULLNAMES5 = Names.map (() = ' Cool Bos '); Console.log (FULLNAMES5); Cool Bos, cool Bos, cool BOSThere is
First, the grammar1. Simple function with one parameter
12
varsingle = a => asingle(‘hello, world‘) // ‘hello, world‘
2. Need to use the parentheses before the arrows for no parameters
123
//user input number, according to the user's choice to print the stars//just playing the star function.Let Chooseone = line ={Let strspace= ""; Let Strstar= ""; //this layer for loop controls the number of rows of stars for(Let I=1; i) {
ES6 can use the arrows to define a function, note that it is a function, and do not define a class (constructor) in this way. =>
First, the grammar
1. Simple function with one parameter
var single = a => a
("Hello, World")//' Hello, World '
The JavaScript arrow function is an easy way to write function expressions introduced in ECMAScript 6. Typically, in JavaScript, you can create functions in two ways:
function statements.
An expression of the function.
You can
In this paper, the operating environment of the demo is Nodejs, reference: let Nodejs support ES6 lexical----Babel installation and use ;Default value of the function:If there are parameters, then use parameters, if there are no parameters, then use
Tip
First of all, I know this article is very boring, nothing more than the this in JS, and there have been thousands of articles have written this part of the content;However, I still want to write a about JS in this article, is a summary summed
In js, the context of this is always unpredictable. In many cases, bugs are always confused. In fact, it is okay to know how to execute it in different situations. Global execution
First, let's look at what this is in the global environment:
First.
Learn how to use this in JavaScript and how to use javascript.
Tip
First of all, I know this article is boring. It is nothing more than this in js, and thousands of articles have already written this Part;However, I still want to write an article
We often encounter some pits in the callback function:var obj = { name: ‘qiutc‘, foo: function() { console.log(this); }, foo2: function() { console.log(this); setTimeout(this.foo, 1000); } }obj.foo2();Executing this code we will find
In JS, this is the context in which it is directed according to its scope.Global executionIn the global context, let's see what it is:You can see that a Window object is returned.In node, this returns the global object.Summary : It executes the
This is one of the most intriguing features in JavaScript, like the tenses in high school English, such as the passive tense, past tense, present tense, past time, and, no matter how many times you miss, the next time you may be mistaken. This
This can be said to be one of the most intriguing features in javascript, just like the tenses in high school English, such as the passive tense, the past tense, the present tense, and the past tense, no matter how many times you miss it, the next
Executing in the globalLet's see what this is in the global.first, in the browser :Console.log (this); // Window {speechsynthesis:speechsynthesis, caches:cachestorage, Localstorage:storage, Sessionstorage:storage,
To tell you the truth, since the arrow function has been out, there is no need to worry about the this problem, and on the one hand, to simplify the code, the arrow function is loaded force artifact.There are a few points to note in the arrow
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.