JS - Function Methods

6 flashcards · Programming Languages · by zeckdude

  1. What is the function property to get the number of arguments expected by a function? Provide example using the supplied code: function myFunction(a, b) { ... }

    myFunction.length // returns 2

  2. What is the syntax for defining default parameters on a function? Provide example using the supplied code: function myFunction() { ... }

    function myFunction(a = "value") { ... }

  3. What is the keyword for accessing all arguments passed to a function? Answer the following question: What data type are the arguments returned in when accessed via the keyword?

    Answer: It is an array-like object. arguments

  4. What is the function method to call a function while setting the context that will be available to the function? Hint: This method accepts an array of values for the arguments being passed to the function being called. Provide example using the supplied code: function myFunction(a, b, c) { ... } Call the function with the context of window and the arguments of 1, 2, and 3

    myFunction.apply(window, [1, 2, 3]) // returns undefined

  5. What is the function method to call a function while setting the context that will be available to the function? Hint: This method accepts a list of values for the arguments being passed to the function being called. Provide example using the supplied code: function myFunction(a, b, c) { ... } Call the function with the context of window and the arguments of 1, 2, and 3

    myFunction.call(window, 1, 2, 3) // returns undefined

  6. What is the function method to return a function while setting the context that will be available to the function? Provide example using the supplied code: function myFunction(a, b, c) { ... } Return the function with the context of window and the arguments of 1, 2, and 3

    myFunction.bind(window, 1, 2, 3)