This question already has answers here:
Why would you ever call .call() on Observable functions?
(3 answers)
Closed 5 years ago.
I have seen in the ngrx source, for example, that they use call.
pluck.call(route.params, 'id');
What is the difference between the above code and:
route.params.pluck('id');
When do we need to use call when using observables?
When you use call, you explicitly give the context of the function - to which this refers in the function.
See the difference between calls.
function test() {
console.log(this.n);
}
const obj = { n: 'Your Name' };
test();
test.call(obj);
Better. Thanks to #cartant. Based on his comment editing the answer
The call mechanism was recommended for library authors, so that they don't patch Observable.prototype with operators that library clients could accidentally depend upon. If a library were to patch and later remove operators, client code could break. It was tedious for library authors and it can now be avoided using pipe and pipeable/lettable operators
Related
This question already has answers here:
Standard conventions for indicating a function argument is unused in JavaScript
(6 answers)
Closed 9 months ago.
Specifically, I am using react-semantic-ui's Select Form, which takes an onChange function. So I have defined my function as such:
const handleInputChange = function handleInputChange(event, data){// does something with data}
(don't mind the ugly syntax, that's airbnb style guide)
I am doing something with data, but I am not using the event parameter at all. What would be the best practice in this case, since it is generally a bad practice to declare a variable without using it?
You must declare a parameter for every argument before the one you want to use.
The idiomatic approach to marking a parameter as unused is to prefix its name with an underscore.
function handleInputChange(_event, data){
Some linters will recognise this and not emit a "unused variable" warning.
For example ESLint has a argsIgnorePattern option (but it also has a args option that lets you specify after-used).
Actually by using Js es6 you could code without unused argument:)
function handleInputChange(...[, data]) {
console.log(data);
}
handleInputChange('event', 123);
And bellow is a similar question you could check out:)
Standard conventions for indicating a function argument is unused in JavaScript
This question already has answers here:
AsyncFunction is not defined, yet MDN documents it's usage
(2 answers)
Closed 1 year ago.
I am trying to define an AsyncFunction (please note the big F) in node as described here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction
But I get:
AsyncFunction('var1', 'var2', 'let test=var1+var2') (1, 2)
ReferenceError: AsyncFunction is not defined
Creating a normal Function object works fine. Does NodeJS not support AsyncFunction?
I think the question you're fundamentally asking is what is the syntax for defining an asynchronous function, and not really trying to invoke the constructor of that object.
if so then:
synchronous:
function foo()
asynchronous:
async function bar()
Update:
After understanding your comments, I encourage you to re-read the spec:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction
It clearly describes that it is not a global-scope object. The examples listed below it as well, also describe it's usage which also reinforce the idea that it must be assigned (locally) before usage, so there is a definition of it.
This question already has answers here:
What do multiple arrow functions mean in JavaScript?
(7 answers)
Closed 3 years ago.
Below is a code snippet which I came across in one of the blog for redux.
This snippet explains redux-thunk. But I am trying to make sense out of the weird syntax
return ({dispatch, getState}) => next => action =>{}
I tried a google search with this syntax but did not get much help. I am not understanding how next and action objects are getting their values. Can someone please help me understand what's going on here?
Chaining of functions in this way is usually to enable a user to bind arguments to a final function in a controlled fashion, rather than having to supply them all in one go.
In effect it is an API design choice for a piece of functionality.
({dispatch, getState}) => next => action => {}
...is equivalent to:
function({dispatch, getState}) { // destructure two properties into arguments
return function(next) {
return function(action) {
// this function has access to `dispatch`, `getState`, `next` & `action`
}
}
}
Note that the "binding" of the arguments to the inner functions occurs through a feature of JavaScript called closures. Full explanation of closures in JavaScript here.
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.
This question already has answers here:
How does basic object/function chaining work in javascript?
(5 answers)
Closed 8 years ago.
What is the best way to implement method chaining in javascript?
function CONSTRUCT(){
this.func1=function(insert_ob){insert_ob.appendTo=this;/*other code*/};
this.func2=function(...){};
this.func3=function(...){};
}
function My_Object_Creator(){this.appendTo=null;}
object1=new CONSTRUCT();
my_object=new my_Object_Creator();
object1.func1(my_object).func2().func3();
I know I can adjust those functions to in the constructor to achieve it but I want to know if there are some well-known methods or patterns one should follow while creating method chaining possibility in javascript?
The most basic way to method chain is to return this (or a reference to the object).