Should I not use var in ES6? [duplicate] - javascript

This question already has answers here:
What is the use case for var in ES6?
(5 answers)
Closed 6 years ago.
I am sorry if this is asked already but as I am introducing myself in ES6 on some tutorials, the instructor is saying that the var statement should be avoided. It just feels wrong but, is there any good reason for why should it be avoided?

var is on ES6 for legacy reasons. In theory, the let statement is better since it behaves more predictably on block scopes, but it won't work with more outdated interpreters. So, if you're coding with only ES6 in mind, go for let.
EDIT: Also, keep in mind that if you're new to JS, you'll likely find more learning material using var, so keep that in mind.

Related

The role of this.async(); in Javascript? [duplicate]

This question already has answers here:
What does this.async() do in JavaScript
(3 answers)
Closed 3 years ago.
This might be a possible duplicate of this question What does this.async() do in JavaScript but that question is under a different circumstance and the accepted answer got negative vote, hence wrong or original question unaddressed. Also the most liked answer did not clarify it, the answerer also said so in his answer.
What is the actual use of this.async(); is JS? Is it part of the core JS language or a conventional function used by libraries? I couldn't find any documentation on this.
The this.async() is not standard (core) JS function
this.async();
If anybody else is wondering about this. I just found this link which kind of sums it up and details the use case.
https://github.com/sboudrias/run-async#readme
And Grunt also use this same technique to know that an async task has finished.
https://gruntjs.com/inside-tasks

How to use eval() safely in JavaScript for education app? [duplicate]

This question already has answers here:
Is it possible to sandbox JavaScript running in the browser?
(15 answers)
Closed 3 years ago.
I'm aware similar questions have been asked before, but my use-case is a little different.
I'd like to create an educational app similar to LeetCode, CodeWars, etc. where users can type in their own functions to solve algorithms.
Right now, I'm focusing on JavaScript, so the code could be evaluated on client-side.
I want to know, what considerations should I take into account to use eval() and new Function() safely?
Or is there a better alternative?
Luckily you're not alone, and someone invented the wheel before you. You can check out NeilFraser/JS-Interpreter or sandbox.
There may be even better documented/implemented solutions that I'm not aware of, but it's a good start.

Reason behind strange iterator syntax on Arrays [duplicate]

This question already has answers here:
What is the motivation for bringing Symbols to ES6?
(7 answers)
Why do you access Symbol.iterator via brackets?
(3 answers)
Closed 3 years ago.
I recently learned that to get an iterator from an Array, you have to access it using syntax that I have never seen before: let iterator = myArray[Symbol.iterator]()
It seems to me that implementing Array.prototype.getIterator() would have been a more idiomatic way to go, but I must be oversimplifying or just not understanding the significance of accessing this property of arrays in this particular way.
In attempting to deepen my understanding of the inner workings of JavaScript I was hoping someone could explain this diversion from a more traditional syntax.

What is valid syntax of and what are uses of # at javascript? [duplicate]

This question already has answers here:
What does the at symbol (#) do in ES6 javascript? (ECMAScript 2015)
(3 answers)
Closed 6 years ago.
Have viewed
#propertyOrIdentifier // ? What does this mean and do?
used within apparent plain objects or class assignment at Questions and Answers at stackoverflow.
What is the # symbol or character in javascript? What are valid uses?
It is called a decorator.
https://github.com/wycats/javascript-decorators
Medium - Exploring es7 decorators
It doesn't have any special meaning in vanilla JS.
It's likely that wherever you are seeing it it is simply being used as a naming convention, similar to doing something like adding a preceding underscore for private variables _example

what are Mixins in the React.js framework? [duplicate]

This question already has answers here:
What is a mixin and why is it useful?
(18 answers)
Closed 6 years ago.
I am reading Reactjs documentation and came across mixins,but i dont understand mixins clearly.What are mixins and what are they used for?
Here is a link that will explain the concept in more detail.
https://javascriptweblog.wordpress.com/2011/05/31/a-fresh-look-at-javascript-mixins/
"Mixins are a great compromise, allowing entire functional units to be borrowed and accessed with minimal syntax and they play very well with prototypes. They offer the descriptive prowess of hierarchical inheritance without the brain-cracking issues associated with multi-tiered, single-rooted ancestry."

Categories

Resources