JavaScript - Unset argument [duplicate] - javascript

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.

Related

_ as a variable in JavaScript function [duplicate]

This question already has an answer here:
Understanding arrow function parameters
(1 answer)
Closed 1 year ago.
I came across this function:
const rotatedTetro = matrix.map((_, index)=> matrix.map(col => col[index]))
What does the _ passed in mean?
There’s nothing special about underscore as a variable name. It’s just a convention often used to indicate that the argument won’t be used, but it needs to be present because you need the args that follow it.
It’s a way of indicating the code is “skipping over” that argument.

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

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) ?

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.

Arguments of self executing function [duplicate]

This question already has answers here:
How does this JavaScript/jQuery syntax work: (function( window, undefined ) { })(window)?
(5 answers)
Closed 9 years ago.
(function (global, undefined) {
... some code which doesnt use arguments array
} (this));
I often see module pattern done in this way.
I really question why there's a second argument undefined?
Are these examples buggy or is there a special meaning of undefined here?
undefined is a global property that is widely used. In older versions of JavaScript it is possible to change the value of it (for example, to true). This generally breaks everything.
By changing its scope to be local to the "module" (i.e. the function), other modules are prevented from interfering with it.
This allows code to safely use undefined instead of having to use global.undefined.
MDN Reference

Categories