Significance of defining custom methods to window object in javascript [duplicate] - javascript

This question already has answers here:
What's the difference between a global variable and a 'window.variable' in JavaScript?
(5 answers)
What is the purpose of the var keyword and when should I use it (or omit it)?
(19 answers)
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
Closed 5 years ago.
I am working on a project in javascript and while going through design pattern concepts came across following point.
I have a sample code as below:
window.customfun = (function(){}())
Here i have two questions:
1) Can we not simply define a global function as below:
customfun = (function(){}())
2) (function(){}()) is an anonymous function with its own evaluation environment but i have learnt that to be implemented as : (funtion(){})()see the position of parentheses around couple of parentheses. I found that both are working so i wanted to know if there is any difference between them?

Sure, you can define global functions
No, there is actually no difference.
Is there a difference: (1+(1)) === (1) + (1) ?

Related

JavaScript - Unset argument [duplicate]

This question already has answers here:
Are variables statically or dynamically "scoped" in javascript?
(7 answers)
What is the scope of variables in JavaScript?
(27 answers)
What is lexical scope?
(21 answers)
Lexical Scope in JavaScript
(4 answers)
Closed 8 days ago.
I am working on a large library and I want to reduce conflicts in the code as much as possible. I have a function like this:
((privateArgument) => {
var transformed = privateArgument.someProperty;
userCallback(); // This should only have access to transformed
})({someProperty: 'Hello, world!'});
I should mention that I tried to simplify the example as much as possible to illustrate my intention. In reality, I have no control over the value passed to the function ({someProperty: 'Hello, world!'}).
And an example of the userCallback:
() => {
console.log(transformed); // Can be used here freely
console.log(privateArgument); // Can unfortunately also be accessed
}
I have tried the following:
Setting privateArgument = undefined: breaks access to transformed and does not throw the native "undefined" error
Overriding the privateArgument by defining a new let privateArgument and scoping the userCallback with {}: gives me an error, saying that I cannot redefine it
Passing transformed as an argument to userCallback: does nothing to address the issue
Please note that I am specifically asking about arguments, not regular variables such as var and let. I am aware that the issue regarding normal variables has already been addressed in other questions. However, the answers to those questions are not helpful for this particular issue.
If anyone knows of a scoping mechanism I can utilize for this or how to completely get rid of the privateArgument (so that it throws the native error and can be re-declared in the userCallback) it would be greatly appreciated.

Javascript square brackets around method name [duplicate]

This question already has answers here:
What do square brackets around a property name in an object literal mean?
(2 answers)
Closed 5 years ago.
In the RxJs doc I found following code snippet:
[rxSubscriberSymbol]() {
return new SubjectSubscriber(this);
}
Its part of the Subject source code and is the first method right after the constructor.
So what do square brackets mean in this context?
those are symbols, which is very similar to defining properties but gives different accessibility and testability functionality and they are completely unique,
you can read a lot more about metaprogramming here,
Metaprogramming in ES6: Symbols and why they're awesome

How does the new Javascript syntax work regarding functions [duplicate]

This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 5 years ago.
I'm not sure what this is called in Javascript:
exports.withLocalCallback = () => {
return [1, 2, 3].map(x => x + 1)
}
I haven't seen this before and I can't even think of a way to google it. Can someone explain what's happening here?
It's arrow functions, and they work almost the same as normal js functions, with the difference that 'this' is bound to the scope in which the function is defined. So you don't need to bind functions to access the correct object.
The difference is none if you don't need 'this', except is another syntax, which looks more like functional language functions.

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.

jquery (but probably more of a javascript) construct explanation [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Why define an anonymous function and pass it jQuery as the argument?
(5 answers)
Immediately-Invoked Function Expression (IIFE) In JavaScript - Passing jQuery
(1 answer)
Closed 9 years ago.
Could someone explain in layman's terms what the following construct is doing:
(function(jQuery) {
// some javascript code here
})(jQuery);
of course, any "read this post", or "read this link", or even "don't do that! this is a better way" kind of responses are also more than welcome.
This code is creating an anonymous, self-invoking function with a reference to a variable called jQuery from the parent scope passed as first argument.
This syntax is often use to "namespace" the code you are writing to avoid the pollution of the global scope, very popular with JavaScript Module pattern.

Categories