javaScript - pass object as function argument

t411tocreate

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?

v-andrew

ES6 supports parameters destructuring. You can use:

function bar({a}){
    console.log(a)
}

However usually it is useful when you have multiple parameters:

// you pass option to a function old way
function oldOps(option){
    var protocol = option.protocol;
    var method = option.method;
    var port = option.port;
    console.log(port);
}
// new and more readable way
function newOps({protocol, method, port}){
    console.log(port)
}

Only old IE doesn't support it.

But when I pass an object directly, it doesn't work:

function bar({a: 0}){
  console.log(this.arguments.a)
}

You cannot pass parameters this way or make initialization of a default parameter. Furthermore, this in you case will refer to the parent object, so this.arguments.a doesn't make sense as in most cases it will refer to window object.

With parameters destructuring you may use default parameters, so your code will look:

function bar({a = 0}){
    console.log(a)
}
bar({}) // 0

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:

function bar({a = 0} = {}){/*...*/}

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

javascript/typescript - force to pass object as function argument by eslint rule

From Dev

JavaScript throws: "is not a function" when I pass object as argument

From Dev

Cannot pass array object from map function as argument javascript

From Dev

Javascript function of object as argument

From Dev

Pass a function with parameter as an argument to another function in JavaScript

From Dev

How to pass a function as an argument to a custom function in Javascript

From Dev

Accessing an object in JavaScript with argument of function

From Java

How to pass an object as an argument to the function mousePressed() in Processing

From Dev

How to pass an object as default argument to function in ecmascript?

From Dev

Pass Python object as argument to function in "parfeval"

From Dev

How to pass String Argument to javascript function

From Dev

Unable to Pass Two Parameters as Argument to Javascript Function

From Dev

How to pass argument to javascript anonymous function

From Dev

Unable to pass an array as function argument in Javascript

From Dev

javascript function is not being called if i pass a argument

From Dev

How to pass a string as an argument to a javascript function

From Dev

How to pass a string as argument JavaScript function?

From Dev

Pass php object to a javascript function

From Dev

Javascript pass function in object value

From Dev

Pass event object in a javascript function

From Dev

Pass object into function as parameter in Javascript

From Dev

Pass object to function in html in javascript?

From Dev

JavaScript Pass Argument to Function keeping Argument Data Intact

From Dev

Passing argument to the function inside the object in Javascript

From Dev

Passing javascript object as an argument to a function - typescript equivalent

From Dev

How to pass an argument to each function inside a compose function in JavaScript

From Dev

How to pass a self invoking function as a function argument in JavaScript?

From Dev

How to pass a function argument to a created instance inside the function javascript?

From Dev

Python - how to pass to a function argument type of a class object (typing)

Related Related

  1. 1

    javascript/typescript - force to pass object as function argument by eslint rule

  2. 2

    JavaScript throws: "is not a function" when I pass object as argument

  3. 3

    Cannot pass array object from map function as argument javascript

  4. 4

    Javascript function of object as argument

  5. 5

    Pass a function with parameter as an argument to another function in JavaScript

  6. 6

    How to pass a function as an argument to a custom function in Javascript

  7. 7

    Accessing an object in JavaScript with argument of function

  8. 8

    How to pass an object as an argument to the function mousePressed() in Processing

  9. 9

    How to pass an object as default argument to function in ecmascript?

  10. 10

    Pass Python object as argument to function in "parfeval"

  11. 11

    How to pass String Argument to javascript function

  12. 12

    Unable to Pass Two Parameters as Argument to Javascript Function

  13. 13

    How to pass argument to javascript anonymous function

  14. 14

    Unable to pass an array as function argument in Javascript

  15. 15

    javascript function is not being called if i pass a argument

  16. 16

    How to pass a string as an argument to a javascript function

  17. 17

    How to pass a string as argument JavaScript function?

  18. 18

    Pass php object to a javascript function

  19. 19

    Javascript pass function in object value

  20. 20

    Pass event object in a javascript function

  21. 21

    Pass object into function as parameter in Javascript

  22. 22

    Pass object to function in html in javascript?

  23. 23

    JavaScript Pass Argument to Function keeping Argument Data Intact

  24. 24

    Passing argument to the function inside the object in Javascript

  25. 25

    Passing javascript object as an argument to a function - typescript equivalent

  26. 26

    How to pass an argument to each function inside a compose function in JavaScript

  27. 27

    How to pass a self invoking function as a function argument in JavaScript?

  28. 28

    How to pass a function argument to a created instance inside the function javascript?

  29. 29

    Python - how to pass to a function argument type of a class object (typing)

HotTag

Archive