I want to use an object as function argument. When I define an object outside fucntion and then pass it as an argument, it works fine:
var obj = {
a: 0
}
function foo(obj){
console.log(this.obj.a);
}
foo() //0
But when I pass an object directly, it doesen't work:
function bar({a: 0}){
console.log(this.arguments.a)
}
// Uncaught SyntaxError: Unexpected number
An object doesent seem to be a legal argument. How do I fix it?
None of the answers have actually tried to address the issue here so I thought I might have a go.
You ask: "I want to use an object as function argument." And yet your first example doesn't show you doing this. You are not passing in an object as a function argument. What you are doing is declaring a variable under
window
(assuming this is browser code) and then logging that variable's value to the console. Becausethis
is context andthis
is defined by the way the function is called, in this instance the context iswindow
and so your code works.You second code example compounds the errors you're making in the first example.
arguments
is scoped locally to the function. It doesn't needthis
in front of it. And you can't access it like it's an object becausearguments
is an array-like structure. You would access it like this:arguments[0].a
assuming you're passing the object into your function like this:bar({a: 1})
.JavaScript context and scope can be a very difficult concept to get your head around. I'm a pretty seasoned JS developer and it still catches me out occasionally, so don't be disheartened. This article is very good at explaining context (and function scope) and I think you would benefit from reading it.
ES6 supports parameters destructuring. You can use:
However usually it is useful when you have multiple parameters:
Only old IE doesn't support it.
You cannot pass parameters this way or make initialization of a default parameter. Furthermore,
this
in you case will refer to the parent object, sothis.arguments.a
doesn't make sense as in most cases it will refer towindow
object.With parameters destructuring you may use default parameters, so your code will look:
Still, any efforts to call it without parameter will result in error as JS will try to resolve property of
undefined
You may use another default parameter assignment to resolve the issue. When you really want to call
bar()
without parameters and have default value for destructured parameter you should use something like:Just don't forget that it is not widely supported by browsers, so you will have to use transpiler to convert your ES6 code one supported by browser.
Most popular transpilers are Babel and Typescript.
Cause this has nothing todo with the variables passed. Dont use it here. Simply do:
First, it appears that you are combining how to declare a function parameter with how to call a function and pass it a parameter with this code:
Next, when accessing arguments, just use the parameter name, not
this
.this
is used to reference the invocation context object, not function parameters. The invocation context object is essentially the object that was responsible for causing the function to be invoked in the first place. It can also point to an object that was explicitly set to replace the native object that would have been the context object. And, in the right circumstances, it can point to the Global (window
) object or beundefined
.this
is typically confusing for a lot of JavaScript developers. Here's a link to more information onthis
.So your code should be:
Now, you can in fact supply a default value for a function and this would be how to do that: