The documentation of some JavaScript APIs shows the following snippets as an example of how to invoke some function:
<button type="button" onClick="foo.DoIt(72930)">Click</button>
<button type="button" onClick="foo.DoIt(42342::37438)">Click</button>
:: is obviously used here to allow either one or two arguments to be passed to the function.
What does :: do in JavaScript?
And how does the function know if one or two values were passed? How does it read them?
On closer look, the examples show other weird stuff like
<button type="button" onClick="foo.Bar(72//893)">Click</button>
<button type="button" onClick="foo.Qux(425;1,34::)">Click</button>
At least the // looks just wrong.
So I guess it's not some fancy new syntax that I'm not aware of, but maybe the examples are just missing quotes around a single string argument.
It was certainly not the case at the time of your question, but right now :: is a valid ES7 operator. It's actually a shortcut for bind.
::foo.bar
is equivalent to
foo.bar.bind(foo)
See an explanation here for examples:
Nothing. It is a syntax error.
>>> alert(42342::37438)
SyntaxError: missing ) after argument list
:: has nothing to do with the number of parameters. You can do that already in JavaScript with a normal comma:
function SomeFunction(param1, param2) {
//...
}
SomeFunction('oneParam'); // Perfectly legal
Also, based on Tzury Bar Yochay's answer, are you sure you're not looking at something like the following?
$('this::is all one::parameter'); // jQuery selector
In which example did you see that? So far, JavaScript does not have a double colon operator!
The double colon replaced the single-colon selectors for pseudo-elements in CSS3 to make an explicit distinction between pseudo-classes and pseudo-elements. But that is CSS3, not JavaScript! Not At ALL!
It must be a typo for
<button type="button" onClick="foo.DoIt('72930')">Click</button>
<button type="button" onClick="foo.DoIt('42342::37438')">Click</button>
It could be using ECMAScript for XML (ECMA-357 standard) which would imply the double quotes are a XPath operator.
See ECMAScript for XML
I am guessing that the parameter list for foo.DoIt() is generated by code, and one the values was empty.
Perhaps it's a typo, and the whole thing is expected to be in quotes.
Related
So I created the fiddle: https://jsfiddle.net/4ex7uh8g/
The problem is I don't understand the syntactic differences in the particular line of code of that fiddle.
Take a look at 24 line in JavaScript tab. Here it uses such syntax: <div style={{...spanStyle}}> also I tried using such syntax as: <div style={spanStyle}> and it also works fine, and when I inspected the style properties are injected identically, so for me it looks like everything is the same, just the difference in syntax, but I'm not really sure, could I be missing something or misunderstanding when choosing one over another?
I also did read this thread: What do these three dots in React do?
It explains some things about three dots operator, I'm quite familiar with this spread operator, but still in my provided examples I don't see what difference that spread operators does, it doesn't separate attributes and values it works the same as {spanStyle}, so for me {{...spanStyle}} and {spanStyle} act identical?
Would be really grateful if you could point out the difference if there's any and when and why should I use one syntax over another.
When using a single pair of braces, you basically tell JSX you're going to embed a JS expression. When you have a second pair within the first, you're creating an inlined object literal(as you're already in JS context). When you specify styles through the style property in JSX you have to supply an object, hence the double braces.
{{...spanStyle}}
Just creates a new object literal and takes all the properties from spanStyle using the spread operator (...) so as you noticed already you will get absolutely the same result as simply doing {spanstyle} but with the overhead of creating an object clone.
First encasing brackets is for the JSX specification for javascript evaluated portions:
<div style={ Javascripty stuff in here }
So, first brackets starts up javascript mode essentially.
Next set of brackets is in javascript land, which in javascript {} means new empty Object
Last piece of the puzzle is the spread operator ....
When you use the spread operator on an object within the declaration of another object, it sucks all the properties out of that object and applies it to the new object you are calling:
<div style={{...anotherObject}} />
So, if you remember the first brackets activates javascript stuff, it all starts making more sense.
What you are looking for is the spread operator:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
With this syntax you can create a new object, and add some prop overriding any original object prop.
Example:
const spanStyle = {width: '100%', height: '400px'};
<div style={{...spanStyle, height: 'auto'}}>
If you dont need override anything, just skip spread!
The project I'm working on uses double quotes for strings in HTML and JavaScript by default.
This has been decided and there is nothing I can do about it.
What hasn't been decided however, is how we should deal with nested quotations.
There are three options I can think of:
Begin with outermost quote & alternate.
element.innerHTML = "<a href='...'></a>";
<button onclick="alert('...')"></button>
Begin with innermost quote & alternate.
element.innerHTML = '';
<button onclick='alert("...")'></button>
Escape everything.
element.innerHTML = "";
<button onclick="alert("...")"></button>
Which is the best practice in terms of overall convenience, style, safety, maintainability, portability, etc.? And please suggest a better way if there is other than the above.
I think the examples you gave are structured a little weird. Regardless, here's another option that I think you'll like better: template literals. You use backticks to define them. However, to use it effectively, you'll have to define your function outside of the string you passed into the HTML parameter for onclick.
Here's an example:
<button onclick="foo()">Click Me!</button>
<script>
function foo() {
alert(`"Hello"`);
}
</script>
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does “javascript:void(0)” mean?
I want to ask a few questions regarding javascript:void(0)
<input type='submit' name='btn' value='submit' onClick='javascript:void(0)' />
Can you please explain void(0) - is it a built-in function? Does the keyword javascript represent that the code is written in javascript? If there is anything weird that you know about it, please share it with me. Thank you.
void():
This operator allows inserting expressions that produce side effects
into places where an expression that evaluates to undefined is
desired.
The void operator is often used merely to obtain the undefined
primitive value, usually using "void(0)" (which is equivalent to "void
0"). In these cases, the global variable undefined can be used instead
(assuming it has not been assigned to a non-default value).
Note, however, that the javascript: pseudo protocol is discouraged over other alternatives, such as unobtrusive event handlers.
You can read more on this similar thread: What does "javascript:void(0)" mean?
javascript:void(0) can be considered as "Do nothing". Not sure what was intended to be achieved with it here. If you wanted to prevent form submission on button click, you should have used something like
<input type='submit' value='submit' onClick='return false;' />
void is an operator that is used to return a undefined value so the browser will not be able to load a new page. An important thing to note about the void operator is that it requires a value and cannot be used by itself.
It's defining an event handling function that has no body, so nothing gets executed. You will most commonly see it used in the context of an href attribute.
I am using one Javascript function to generate a random Number.
Used a User Parameter(Preprocessor) under the request
Added a Variable: farmeid
Function: ${__javaScript('bam-'+parseInt((Math.random()*1000000),10))}
When I am using the varialble ${frameid}, it is returning no Value.
The problem is that there is a comma in your expression here: ),10 which acts as a delimiter for function parameters. If you can remove this ,10 bit so your expression could look like:
${__javaScript('bam-'+parseInt(Math.random()*1000000),)}
This would be successfully evaluated by __javaScript function.
If this 10 bit is a must you can use BSF PreProcessor with the following code:
vars.put("frameid",'bam-'+parseInt((Math.random()*1000000),10));
And the easiest way is using __Random function like
bam-${__Random(111111,999999,)}
directly where it's required.
Hope this helps.
It's better to use Jmeter's Random Variable generator.
http://jmeter.apache.org/usermanual/component_reference.html#Random_Variable
Cheers,
Suppose that we have:
<input type="button" name="button" value="Validate Payment" onclick="Validate()" />
We can put in the onclick:
onclick="Validate"
onclick="Validate()"
onclick="Validate();"
My question is what is the difference between these 3 implementation
Will not execute the function, it will return the function (try: console.log(Validate))
Will execute the function.
Will, exactly like #2, execute the function.
So there's no difference between #2 and #3, functionally.
Please note that inline event handlers are bad practice, like mentioned in this comment by jbabey.
The difference between onclick="Validate();" and onclick="Validate()" is non existant. Semicolons are optional in javascript, so your code will be the same.
To understand the difference between onclick="Validate" and the other two, you need to understand a little about the use of parenthesis in Javascript.
Bascially,
onclick="Validate()"
will execute the function "Validate" and return whatever that function returns.
onclick="Validate"
will return the actual function "Validate".
See here for a fuller explaination: javascript syntax: function calls and using parenthesis
The parentheses () after the function name represent 'void', which is - at least by default - neccesary to execute the function properly. In some cases you would need to type in something between those parentheses, which is called 'passing a value' along with the function.
Since this (the 'void' thing) is almost, but not exactly, the same for a lot of similar programming languages (php, Actionscript, etc), and it's kind of a vague thing, I can't tell you exactly what this is for in JS though.
The difference between
onclick="Validate()"
and
onclick="Validate();" is in terms of functionality nothing. The semicolon ; separates pieces of code from each other, so it would be required if you would call another function after the Validate() function. In this case, there's no difference betweeen onclick="Validate()" and
onclick="Validate();".
There's no difference between them.