bmkmanoj / js-by-examples Goto Github PK
View Code? Open in Web Editor NEWLearn JavaScript by examples
Learn JavaScript by examples
Is there an advantage to hosting the examples in an example folder versus the wiki in the repo?
I think that would be a great idea to add some example to help who is a noob or to help who have no experience with Javascript.
Hi Manoj,
We have to update functions_inside_conditional_blocks.md
New JS engines obaying conditional functional declarations.
Run the code of functions_inside_conditional_blocks.md in console:
output will be:
innerFunc: Inside if
innerFuncExpr: Inside if
Hi there!
Always good to see people wanting to help others! And it's a nice little collection :) .
However, there is a little misinterpretation concerning Functions inside methods and this variable and in this particular case, in a forEach function, there is no need to 'hard bind' any context.
The function in //1
is the callback of a forEach loop function.
A forEach function (Array.prototype.forEach) can take 2 arguments even if the second, the "this" argument, is somewhat less known. See Array.prototype.forEach
The snippet
...
this.myNickNames.forEach(
function (nickName) { // 1
console.log(this.myName +' is also known as '+ nickName); // 2
}.bind(this)
);
...
could be replaced with
...
this.myNickNames.forEach(
function (nickName) { console.log(this.myName +' is also known as '+ nickName); },
this // 2nd arg
);
...
How that's working? Let's try to build a minimal custom forEach function :
function forEach(array, callback, thisArg) {
var length = array.length;
var i = 0;
for(i; i< length; i++){
// Here, a forEach function applies the callback to thisArg
callback.apply(thisArg, [array[i], i, array]);
}
}
So, I see a lot of people using var self = this
statements when using foreach, map and others arrays manipulation functions, and that's not necessary.
And even better, we have now arrows functions with es2015.
Your snippet could be rewritten like this
...
this.myNickNames.forEach(
nickName => console.log(this.myName +' is also known as '+ nickName); // Yeah!
);
...
That's all. Hope it can be useful!
And happy Js coding! ;)
let var const
function hi () { alert("hi") }
hi = function () { alert("hello") }
console.log(hi()); //"hello"
better
const hi = function () { alert("hi") }
hi = function () { alert("hello") }
console.log(hello); // Uncaught TypeError: invalid assignment to const 'hi'
let obj = {};
obj = "string"; //"string"
better
const obj = {};
obj = "string"; // Uncaught TypeError: invalid assignment to const 'obj'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.