How to create a pointer in JavaScript [duplicate] - javascript

This question already has answers here:
Are there pointers in javascript?
(6 answers)
Pointers in JavaScript?
(15 answers)
Closed 3 years ago.
A friend of mine told me JS has the best kind of pointers?
I did a bit of research, and everywhere I looked for memory addresses in JS and pointers I found answers like
Its more or less possible
It's more or less Impossible
So which is it?
How can you create a pointer in JS?

There are no pointers in the JavaScript language.
Every variable that contains an Object is actually an opaque reference to that object.
Within the interpreter that reference will take the form of a pointer, but the value of that pointer is not accessible to you.

Javascript do not support pointers.
But you can make use of Object which will serve the purpose of pointers.
Refer the code below for passing parameters to function by reference :
var numbers = {num1:5,num2:8};
var mod = changeNum(numbers);
function changeNum(var Obj){
Obj.num2 += 2;
}
Output:
{num1:5,num2:10}

Related

In JavaScript what is this method called where you might define a variable or property as such variable_name$ref [duplicate]

This question already has answers here:
Rules for unquoted JavaScript Object Literal Keys?
(6 answers)
What characters are valid for JavaScript variable names?
(12 answers)
Closed 1 year ago.
I am seeing it crop up more and more in code I am going through on a new project (can't share due to contractual reasons) where Ill see something like:
{
prop1: value$ref,
$prop2: null
}
I have see ${prop3} before, but never an example without the brackets. Can anyone provide direction as to what the method is, or the operator is or whatever the case?

what is the explanation of this kind of instruction let {max}=Math; [duplicate]

This question already has answers here:
What do curly braces inside of function parameter lists do in es6?
(3 answers)
Closed 2 years ago.
I came accross this line of javascript code
let {max}=Math;
that allows you to do this:
let a=max(1,2) //2
Without using the Math object like this:
Math.max(1,2)
I don't know where to find the documentation of this syntax because all I have is that line of code and not a single clue to help me search on google so anyone knows about this practice ?
This is called Destructuring Assignment and you can find documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
What you see there is called Destructuring assignment.
You'll find plety of examples when you Google for Destructuring assignment JS ;)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
Anyway, in this particular case you assign the property max (a function in this case) from Class Math.
This allows you to just call max directly.
Imagine having an object DestructTest with a property x
Then you can get the property x by just typing const {x} = DestructTest
Instead of const propX = DestructTest.x
JavaScript takes care for you to get the wanted property for you if you just ask for the right name ;)
Hope this makes things a bit more clear.
Cheers

How to see the code of a javascript function? [duplicate]

This question already has answers here:
How can I read ‘native code’ JavaScript functions?
(3 answers)
Closed 3 years ago.
This may be a very dumb question.
I want to see the code of a function (Built in and User defined) in Javascript.
For example :
function hello(){
console.log("hello")
}
hello.toString() // Gives the function definition
'function hello(){\nconsole.log("hello")\n}'
Is there a way to see the native code like Math.random.toString()?
Update: From the comments, Seblor explained that native code cannot be seen.
You could do some string formatting to get a "better" look at your functions. Use this peace of code to get rid of the function name to get just the code.
function justGetCode(funcName)
{
var tempString = funcName.toString();
tempString = tempString.substring(tempString.indexOf("{"));
return tempString
}
But beyond this there is little you can do in terms of digging into native (i.e. browser specific ) code as it is encapsulated. This should work on library functions however.
Now i do not know what you are planning on doing with the returned function, but for fancier function manipulation you can always use in-built reflection mechanisms

Javascript use variable name to thread a function? [duplicate]

This question already has answers here:
Use JavaScript variable as function name?
(5 answers)
Closed 7 years ago.
In GSC, you are able to make a variable become the name of a function that you thread. It looks like this:
variable = "pizza";
[[variable]]();
the engine then reads it like:
pizza();
my question is, is it possible to do that in javascript as easily or do I have to make if/else/switch statements for it?
my question is, is it possible to do that in javascript as easily or
do I have to make if/else/switch statements for it?
If you want to use the safe, fail-proof way, then you can access such variables only in two contexts.
If the variable is in global context, in the case of which, you can do window[variable]();
Else if the variable is a property of an object, in the case of which, you can do obj_name[variable](), basically anything that can be accessed via bracket notation. window is an object too.
Then there's always the dirty way:
You can use highly evil eval like eval(variable + "()") or you can use the Function constructor in the same way. Note however that both the methods can be misused and are highly advised against.

Why is "closed" a reserved word is JS? [duplicate]

This question already has an answer here:
Can't get global variables inside the function (javascript)
(1 answer)
Closed 8 years ago.
Well, after much head scratching this afternoon; I came to realize why my array was coming back as undefined. Despite the word 'closed' not being a reserved JS word; it seems it is some kind of reserved word elsewhere and so an array cannot be called 'closed'.
My question is this; if Javascript isn't reserving this word - what is? The browser? The OS? I read that one should avoid using it as a naming convention for variables / objects but I don't understand what else is trying to use it.
Any insight would be much appreciated.
<html>
<head>
</head>
<body>
<script>
var greatArray = [];
var closed = [];
alert(greatArray.length);
alert(closed.length);
</script>
</body>
</html>
It's not so much that Javascript has reserved it as that it is reserved because of where Javascript is used. Consider this:
if (myWindow.closed)
Therefore, using closed as a name for a global variable must be avoided. You can use it as a local variable, though.
As mentioned by T.J. Crowder:
It's a predefined property on window that you can't redefine, and since all properties on window are globals

Categories

Resources