I study the following code to log:
console.log.apply( console, arguments );
What is the purpose of apply()
here?
Why not just console.log("message", arguments)
?
Prompt Box in JavaScript To ask for user input in JavaScript, you can use the built-in prompt box. To show a prompt, use the prompt() method.
JavaScript is a popular web scripting language and is used for client-side and server-side development. The JavaScript code can be inserted into HTML pages that can be understood and executed by web browsers while also supporting object-oriented programming abilities.
JavaScript is a lightweight, interpreted programming language with object-oriented capabilities that allows you to build interactivity into otherwise static HTML pages. The general-purpose core of the language has been embedded in Netscape, Internet Explorer, and other web browsers.
console.log("message", arguments)
calls log
with two arguments, "message" and the array-like object arguments.
console.log.apply( console, arguments );
calls it with n
arguments, where n
is the length of the array-like object arguments. In other words, arguments is unwrapped into individual arguments. The context of the method is console
. E.g.:
function foo(a, b, c)
{
console.log.apply( console, arguments );
}
foo(1,2,3);
is roughly equivalent to:
console.log(1,2,3);
The apply()
function calls another function with a given this
value and arguments
provided as an array.
The reason for an implicit func.apply(obj, args)
is to make sure that within func()
, this
refers to obj
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With