What is (function e(t,n,r){ ...}) in Javascript? [duplicate] - javascript

This question already has answers here:
What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?
(10 answers)
Closed 6 years ago.
I am studying the List.js Framework.
This is a starting code. If I remove it(even remove the surrounding "()" of function e), Code is not working.
What is this? I think it is already defined in Javascript method.
(function e(t,n,r){
function s(o,u){
if(!n[o]){
if(!t[o]){
var a=typeof require=="function"&&require;
if(!u&&a) return a(o,!0);
if(i)return i(o,!0);
var f=new Error("Cannot find module '"+o+"'");
throw f.code="MODULE_NOT_FOUND",f
}
var l=n[o]={exports:{}};
t[o][0].call(l.exports,function(e){
var n=t[o][1][e];
return s(n?n:e)
},l,l.exports,e,t,n,r)
}
return n[o].exports
}
var i=typeof require=="function"&&require;
for(var o=0;o<r.length;o++)
s(r[o]);
return s
}
)

It is a minified version of javascript (which does not make any actual difference, other than file size, and readability) so really, it is actually very much normal javascript code. However, written without the non compulsory syntax, unneeded whitespace, shorter variable names, all in efforts to reduce file transfer time.

Related

Arrow function confusion [duplicate]

This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 4 years ago.
I was doing a question on codesignal and when I couldn't get the answer myself for all tests, revealed the answers and saw this one; while I understand what it's trying to do (make sure positions are not divisible by n, and if it is increment n by one) I'm having trouble understanding the arrow function syntax and, rewriting it myself from their code.
function obst(inputArray) {
for (var n=1;;n+=1) if(inputArray.every(x=>x%n)) return n;}
In Javascript, every function written like this:
function(args) {
// DO SOME STUFF
}
can be written like this:
(args) => {// DO SOME STUFF}
In your case, the method .every() expects a function, and
function(x) {
return x%n;
}
is written as
x => x%n
inputArray.every(x=>x%n)
is the same as
inputArray.every(function (x){
return x%n
}))
( except for the way the 'this' keyword works )
For any doubt about Javascript language, i recommend the You Dont Know Js series
written by Kyle Simpson.
It's a very enlightening book.
Tip: When you write a code, ask question to youself like:
what i'm doing?
assigment means equality?
what are the methods that can i use in string variables?
And something like that.
Stimulate yourself to know what in the actual fudge, you are doing.
Cheers!.

syntax for the module pattern in Javascript [duplicate]

This question already has answers here:
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
Closed 6 years ago.
I am learning module pattern in Javascript, and have come across these two ways to create a module:
var Module = (function () {
// code
})();
var Module = (function () {
// code
}());
is there a significant difference in these two approaches? if not, which is considered the better practice? Thanks.
Both are the same. The outer round braces are forcing the inside code to be evaluated as an expression. This means that in both cases the function code is treated as function-expression as well. And then this function is immediately executed due to () braces.
So, it should be exactly the same from the point of view of JS interpreter todo list: 1) get function expression, 2) execute it right away.
It only differs from aesthetic point of view - which way it looks more natural for you.

JavaScript function evaluating itself and calling itself at the same time [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 7 years ago.
I am learning about JavaScript functions on MDN
I came across an example that looks similar to this:
var saySomething = ( function(){console.log("hello")} )();
I have also seen this in the jQuery source.
I have not found any explanation of this style of function calling/definition on the MDN function reference.
I know from running the code that it calls itself immediately upon interpretation by the JavaScript engine.
Is this the Grouping Operator in action? Where it says:
First evaluate the body of this function and return it
Since the parentheses () immediately follow it it gets called ?
Google "Immediately Invoked Function Expression" or "IIFE".
The general syntax looks like this:
(function(){
// do something here
})():
Sometimes you'll see arguments passed in as well. It's basically used to wrap your code so none of your variables leak out into the global namespace.

What's the purpose of closures in MVVM modules? [duplicate]

This question already has answers here:
What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?
(10 answers)
Closed 8 years ago.
I'm working with the following:
define(["knockout"], function(ko) {
var vm = this;
(function() { // I'm tempted to delete this
// init
vm.data = ko.observable("");
// other stuff
})(); // and this
return vm;
});
The person who wrote this said they thought it was a best practice but didn't know why. I understand this is a closure but we don't need any of the "private" functionality that closures provide in this instance, so this just seems like noise to me, but I'm probably overlooking something. What's the point?
You will find a full explanation on that notation in answers to this question: What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?
Short version (sample of accepted answer):
What you're doing when you write (function() { ... code ... })(), is
you're making the code inside a function literal (meaning the whole
"object" is actually a function). After that, you're self-invoking the
function (the final ()). So the major advantage of this as I mentioned
before, is that you can have private methods/functions and properties:
> (function() { var private_var;
>
> function private_function() {
> //code } })()

jQuery's function($) (jQuery) syntax [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
jQuery: What does (function($) {})(jQuery); mean?
I stumbled up on the following code (included in one file) but I just cannot understand what it really means.
(function ($) {
function doSomething1(somedata) {
}
function doSomething1(somedata) {
}
})(jQuery);
Question 1:
What does this syntax mean in the contex of jQuery
Question 2:
How can we call these functions let say from other files such as the HTML index file and other JavaScript files?
Thanks
This syntax is not particularly special to jquery, it's normal javascript. Here simply function
function ($) {
// some code here...
}
(note that it takes argument named $) is called with parameter jQuery (which is, obviously, global object of jQuery framework).
This is usually done when there're several js frameworks on one page (jquery, dojo, prototype, etc) which all redefine global variable $. But with this code, inside doSomething1 or doSomething2 you can always call $('.test') and be certain that call will be processed by jquery, not dojo. Because $ is not a global variable in this case, it's function parameter.
I'm not sure about your question here, but the (function() means it's self-executing,
and you call them by importing the files in the main page and then calling
doSomething1()
Mostly likely that was jQuery plugin: Plugins/Authoring

Categories