"".constructor vs 2.constructor in Javascript - javascript

I've just been experimenting with a little bit of Javascript and got to the point of understanding the inheritance concept.
I am able to get an evaluation for the below code:
"".constructor
//which evaluates to function String()
Ok Cool. But why is it that when I do the below code, there is an error?
2.constructor
//returns an error
Basically both are primitives right?, so should there not be an error for the empty string as well?
Hope someone can give me a good explanation that will help me learn this one better. Looking forward to your support.

You could spend another dot for the decimal point.
console.log(2..toString());
console.log(2.2.toString());
Or wrap the value in parenthesis.
console.log((2).toString());
console.log((2.2).toString());

Related

undefined value used in expression (could be an out of range array subscript )

I am about to have an absolute god damn stroke trying to understand how to write an expression in After effects. It seems pretty much everything I do results in the error "undefined value used in expression (could be an out of range array subscript)" which as far as I understand is an error that means exactly nothing of substance.
I'm pretty sure I got a code exactly like this working a moment ago, but now I get the same error. What am I not understanding?
redx = thisComp.layer("layername").transform.xPosition.valueAtTime(time-1)
yellowx = thisLayer.transform.xPosition
if (redx < yellowx) {
[redx]
}

need direction on how to update this code, language, tutorial?

I can usually Google and find my answer, but I have no idea with this issue on where to even start. If someone could point me in the right direction here as to what language and syntax is being used here, I'd appreciate it.
This is saying "if input1 = 'option1', then output text1 into span1 and if not, input text2.
I'm sorry this is so basic, but need to learn how to edit this but I'm lost without a tutorial or guide.
$('#span1').html(input1='option1'?'text1':'text2');
This is kind of a repost from an earlier question I had where I was accused of trying to get free coding advice so I've made this as basic as possible. I'm not looking for free coding here - if someone could give me a hint as to where I could find more information, I'd appreciate it.
This is a snippet of jQuery - a javascript framework.
$('#span1').html(input1='option1'?'text1':'text2');
$ This is the jQuery "object", and passing a selector argument to it will return a jQuery-wrapped object that you can chain methods to.
#span1 selector argument (may as well be CSS)
input1='option1'?'text1':'text2' Will output "text1" or "text2" depending on wether or not the variable option1 evaluates to true. It's very likely, as u_mulder points out, that this is going to fail. Use two == or three === for comparisons!
html(...) The html method chained onto the output of the selector accepts the new value to write to that element's contents in the DOM.
Suggested reading:
What is a jQuery Object?
Which equals operator (== vs ===) should be used in JavaScript comparisons?
http://api.jquery.com/html/
https://api.jquery.com/category/selectors/
Was able to do it not using that code but a code I'm more familiar with like this:
if(pay_type == 'onetime') {
$('#bank_card_pay_type').html('one time payment');
}
else if(pay_type == 'montly') {
$('#bank_card_pay_type').html('3 monthly payments');
}
else if(pay_type == 'monthly12') {
$('#bank_card_pay_type').html('12 monthly payments');
}

js math equation mystery

I'm new here :)
So, in javascript, I "simply" wanted to solve X in an equation:
My original formula was:
var X = (((((k/(cc**ci))-bk*)/(0.01*kg))-18)/((0.1+0.05*d1)*(280/(w+100)))-sd)
If I recall correctly, it returned some ridiculously high number...
Ok, I guess that was one problem... so, I tried this:
var X = (((((k*1.0/(cc*1.0*ci*1.0))-bk*1.0)/(0.01*kg*1.0))-18)/((0.1+0.05*d1*1.0)*(280/(w*1.0+100)))-sd*1.0)
Now it worked quite well for these simple variables:
cc=0, ci=0, bk=100, kg=100, d1=10, w=100, sd=10
BUT.. when the solution was approaching 0, it suddenly went completely crazy.
In this case, if k=126.4 the solution should be 0. However, I'm getting "7.1054273576" instead. I calculated it with different programs (even my old pocket-calculator lol), and they all say it's 0 --> so I guess my equation should be correct.
I tried k=126.5, which returns 0.119.
and k=126.3, which returns -0.119
These are correct.
So logically k=126.4 should return 0... but it doesn't. It still returns 7.1(...) instead.
I even tried replacing all the variables with the (see above) numbers:
var X = (((((126.4*1/(1*1*1*1))-100*1)/(0.01*100*1))-18)/((0.1+0.05*10*1)*(280/(100*1+100)))-10)
--> THIS STILL RETURNS TO 7.1(...), although it should be 0.
So the problem is definitely not one of my variables.
For these tests I was using an input type="number" object to review the results, if that is of any relevance...
I still can't see the problem, this seems absolutely illogical and is a complete mystery to me. Pls help!
You missed the bit at the end. X is equal to:
7.105427357601002e-15
^
Scientific Notation!
Note the e-15 at the end. That means, in decimal notation, it's
0.000000000000007105427357601002
Or in other words, very close to 0, as far as most uses are concerned. The difference between it and 0 likely comes about by rounding.
The problem was indeed an e-15 at the end. So if anybody else is stumbling across this page they can stop wondering.
A very simple solution: Just use Math.round() in this case to reduce the number of integers behind the comma, so that "e" doesn't get used to describe the number.

Please explain this usage of a colon in javascript

I'm making a library, and I often inspect the result of Closure Compiler's output to see how it's doing things (I do have unit tests, but I still like to see the compiled code for hints of how it could compress better).
So, I found this very weird piece of code, which I never seen before.
variable : {
some();
code()
}
Note: this is not an object literal! Also, there is no ? anywhere that would make it a ?: conditional.
That code is in a regular function block (an IIFE).
variable, in this case, is an undefined variable. There's no code making it true, false, or whatever, and just to make sure, I put a console.log in there and indeed, I get a ReferenceError.
Please do note that I test my code in IE8 too, so this isn't just in modern browsers. It seems to be standard, plain old javascript.
So let's experiment with it. Firing up Chrome's console, I get this:
undeclaredVariable:{console.log('does this get logged?')} // yes it does.
trueValue:{console.log('what about this?')} // same thing.
falseValue:{console.log('and this?')} // same thing.
but then...
(true):{console.log('does this work too?')} // SyntaxError: Unexpected token :
...and...
so?{console.log('is this a conditional?')}:{alert(123)} // Unexpected token .
So what does it do?
thisThing:{console.log('is used to declare a variable?')}
thisThing // ReferenceError: thisThing is not defined
Please, I'd love it if someone could explain to me what this code is meant to do, or at least what it does.
It is a label
Provides a statement with an identifier that you can refer to using a
break or continue statement.
For example, you can use a label to identify a loop, and then use the
break or continue statements to indicate whether a program should
interrupt the loop or continue its execution.
Another common place you see it is when people stick the wonderful and useless javascript: on event handlers.
This is a label (the bit ending with a colon) followed by a block (the code surrounded by the curly brackets).
Blocks usually follow control statements, like if(...) { /*block*/ }, but they can also simply stand on their own, as in your example.
Labels allow jumping up several loops at a time with a continue or break; see the linked MDN page for several examples, such as:
var itemsPassed = 0;
var i, j;
top:
for (i = 0; i < items.length; i++){
for (j = 0; j < tests.length; j++)
if (!tests[j].pass(items[i]))
continue top;
itemsPassed++;
}
Here, top: is a label that code inside the inner loop can jump to, in order to escape to the outer loop.
For the sake of anyone who doesn't know what JSON is, and sees a colon in what might actually be an object, and is trying to figure out what it is, and finds this discussion, a colon is also used in JSON. There is a practice of embedding functions in a JSON object. Which might be confusing (As it was to me) for anyone who happens to see this for the first time. (Everyone isn't born with the knowledge of JSON and JavaScript programmed into their brains.) So if you find yourself at this discussion, and you think that every time you see a colon in JavaScript, that it's a label, it might not be. It might be that it's a colon after a label, OR it might be part of JSON. In fact, a colon in JSON being shown as a string, is a lot more common than a label. JSON in the form of an object, will be displayed as [object Object], with all the content hidden. So, unless the JSON is in the form of a string, and you display an object to the console (console.log(object)) all you will see is [object Object]. It is common practice to write JavaScript code, wrapped in an object. In that case you will see the JSON in the form of code. That's when you'll ask yourself, "What is this? and what is that colon for?" Then you'll find yourself at this discussion, and be told that it's a label, when it's really part of JSON. The topic of this discussion is worded: "Please explain this usage of a colon in javascript", and then the "correct answer" is marked as having to do with a label. The correct answer is that a colon can be used in more than one way. So, if you don't know what JSON is, or think you know (like I did, but didn't really understand) read about it here:
JSON.org
That is just a label.
you can use continue [label name] (or break) in a loop to go to a label.
More explanations of what they are can be seen throughout the interwebs.
it is used for labeling an statement in jsvascript.check more detail here.
the labeled statement can be used with break and continue later.

What is "excessive trickness"?

After reading Douglas Crockford's "JavaScript: The Good Parts" and Stoyan Stevanov's "JavaScript Patterns," I'm trying to determine exactly what 'excessive trickiness' means. They both say that this occours when using either ++ or -- in your code, but can't find a firm definition for this term, either within SO or via a Google search. Any ideas?
You can hear it from Crockford himself here: http://www.youtube.com/watch?feature=player_detailpage&v=47Ceot8yqeI#t=4140s
The simple answer is that ++ and -- act on a variable, and usually do it at the same time as you're doing something else to that variable. For example, can you quickly decide what this does?
y += x++ + ++y; // what is y now?
More StackOverflow discussion: Why avoid increment ("++") and decrement ("--") operators in JavaScript?
It's about the little tricks that are built-in. For example, avar++ is an alias for avar = avar + 1.
JavaScript is filled with operators and other things that make programming easier, but they are just tricks. That's probably what he means.
Also, as Jonathan pointed out: Why avoid increment ("++") and decrement ("--") operators in JavaScript?
Excessive trickiness is subjective. Because of that, there's no firm definition for it.
Some would say the C famous string copy code (while (*p++ = *q++);) is excessively tricky, some would say it's not.
Here is a fairly lengthy discussion with links:
Why avoid increment ("++") and decrement ("--") operators in JavaScript?
I think part of the idea is that when using those operators anywhere but with a single variable on a single line, they become confusing to anyone not in the developer's head.

Categories

Resources