3

I'm trying to read through some source code on the internet, and I'm getting confused because the author defined a function as:

var _0x80a1 = function (x, a) {...}

But then only calls it using statements like this:

_0x80a1("0x0")

How does that work?

3
  • The argument a will be a string "0x0" is clear, and another argument a will be undefined because the author didn't gave any value for _0x80a1 function. Commented Jul 3, 2020 at 2:00
  • Can somebody explain, If we can tackle this particular solution with TypeScript? Thanks! Commented Aug 24, 2023 at 14:09
  • looks like you can by adding a ? to the parameter to mark it as optional Commented Aug 25, 2023 at 16:05

4 Answers 4

4

JavaScript parameters are optional you don't need to pass them. So you can do something like this:

function multiply(a, b) {
  if(typeof b === 'undefined') { 
    b = 10;
  }
  return a * b;
}

console.log(multiply(5));
// expected output: 50

In newer versions of JS you can also do default parameters like this:

function multiply(a, b = 10) {
  return a * b;
}

console.log(multiply(5));
// expected output: 50
Sign up to request clarification or add additional context in comments.

Comments

1

No function "requires" an argument in JavaScript. It's not a strongly typed language.

I might be typing out of my own butt, but I think function's arguments are syntactic sugar in JS. You can always pass any amount of arguments, regardless of the function's "signature", because the only thing that identifies a function in JS, is its name (and the object on which it is called). That is why, the arguments object exists.

So, as others pointed it out, the second, third, or any other argument that wasn't given will simply be undefined.

An answer on this subject with examples

Comments

0

In ,JavaScript function parameters are optional.If your'e not making use of 'a' inside your function then JS compiler don't care about that.If your'e making use of 'a' inside your function then you will encounter some error like "a is undefined".

function (x,a=10){

}

You can set default parameters like this.Now even if your'r passing one parameter to your function it will run without any errors

Comments

0

I was curious so tried to understand this a bit so I could try to answer.

The variable _8x80a1 is a literal bit definition (https://www.hexadecimaldictionary.com/hexadecimal/0x80A1/) representing e.g. 32929 in decimal.

I'm guessing JS internally numbers all functions when its run. This leaves an entire integer (32766) 'vanilla' functions that can be compiled before using a literal as a function name might cause a conflict.

So the 'x' in the function def. looks like it's passing a string, but it might be just calling 'Function #0' in the _8x80a1 var/function. This would make sense if the function contains multiplpe 'sub functions', as then the 'a' variable can be an object collection (e.g. parameters), that can be passed to the sub-function.

Roughtly, I think .. Not used JS for a whilst and just thought I'd try to help answer! ;-) Essentially a compact way to make a toolkit you can copy between projects, and know your references will all work as expected to your tools, without disrupting e.g. jQuery or other scripts. (Wouldn't be surprised if this is how JS is minified actually ;)).

Chris

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.