Why can't I do 1.toString() but I can do var a = 1; a.toString() [duplicate] - javascript

This question already has answers here:
Usage of toString in JavaScript [duplicate]
(3 answers)
Closed 6 years ago.
When I do
1.toSting()
I get an error, but
// javascript
var a = 1;
// or c#
int a = 1
a.toString()
works. Why is it that when a number gets assigned to a variable, it get some special functions?

The . is interpreted as you want a decimal/floating-point literal, not invoking a member.
You can do this in JavaScript
// Option 1
(1).toString();
// Option 2
1.0.toString();
// Option 3
1..toString();
In C#, it appears your only option is (1).ToString(), but the lexer might be smart enough not to need them.

Related

How does "a^=b; b^=a; a^=b;" swap the values of the variables [duplicate]

This question already has answers here:
Swapping two variable value without using third variable
(31 answers)
How does XOR variable swapping work?
(11 answers)
Closed 10 months ago.
I found out a neat trick to swap the values of two variables without having to create an auxiliar variable. I'll use javascript to illustrate it, although I'm guessing it's language agnostic.
let a = 1;
let b = 2;
a^=b; b^=a; a^=b;
console.log(a); // Prints 2
console.log(b); // Prints 1
Can someone explain how does the swap happen? What does ^= do?

We cannot use 1.toString(), but we can use `let a = 1; a.toString()`; Why? [duplicate]

This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Calling member function of number literal
(3 answers)
Closed 2 years ago.
Is this means assignment in js of primitive makes primitive become object automatically?
let a = 1 just be transferred to let a = new Number(1), we know Number is a function, and it's prototype has toString, this makes sense? Is it right?
Finally, we know the primitive of js is stored in stack memory, but if we can only get object by assignment, so is it means only pointer exists in stack? I am confused. Thanks for your answers if you can help me.
You can not call Number.prototype methods directly from a number in digits form, e.g. 1 2 3 4 5 6 7 8 9, you have to wrap them in parentheses.
// This doesn't work
console.log(1.toString());
// This works
console.log((1).toString());

Is it possible to cover a variable with a class JavaScript? [duplicate]

This question already has answers here:
Creating simple constructor in Javascript
(3 answers)
javascript - how to get an object to return a value that is not the object itself
(4 answers)
Closed 2 years ago.
Suppose we have a class called Foo() which takes the value as an argument and has some method method1, metohd2,....
let y = new Foo(56);
console.log(y) // output should be 56 not the class object
y.addOne() // output: adds one to the value
console.log(y) // output should be 57
Can we do this? Not especially with Numbers only, but with other datatypes too?

What Syntax is this in JavaScript [duplicate]

This question already has answers here:
What does variable declaration with multiple comma separated values mean (e.g. var a = b,c,d;)
(2 answers)
Closed 4 years ago.
The left side is a variable name but then on the right side there are two instances separated by comma. I have never seen that before?
var MarkerAnnotation = mapkit.MarkerAnnotation,
clickAnnotation;
It's the same as
var MarkerAnnotation = mapkit.MarkerAnnotation;
var clickAnnotation;
To expand a little, most of the time you'll see it as
var foo, bar, baz;
// or
var foo,
bar,
baz;
...but the introduction of the object property assignment does make it look a bit unclear, especially with the indentation.

Passing variable in function in Javascript [duplicate]

This question already has answers here:
Why would a JavaScript variable start with a dollar sign? [duplicate]
(16 answers)
Closed 5 years ago.
When you pass variables in Javascript functions, what is the difference between '$name' and 'name'.
For example,
var myFunction = function(name){
name.a = "a";
};
var myFunction = function($name){
$name.b = "b";
};
The only difference in your example is the name, it has no special purpose.
However you might have seen the $name styled variables in jQuery examples in which it's normal to name selected elements ($('a')) as, for example, $links.

Categories