I am reviewing my javascript with JsHint, and it always returns an error, saying that a function is never used. Well, this is not entirely true.
I am integrating my javascript on the project index by external link (<script src="myJs.js"></script>), and on the index I call the functions I have on the myJs.js file after the actual file link. So it all looks like this:
<script src="myJs.js"></script>
<script>
myFunction();
</script>
The reason why I call my functions on the actual document and not in the myJs.js file is because I need some PHP functions running in it, and I need to call them from my index file.
The problem here is that JsHint returns a harmful, but annoying error, saying that "myFunction is defined but never used". Is it not aware that my function can later be called within my index? How can I solve this? Should I simply ignore it?
At the top of myJs.js you could try adding an exported declaration with your function name, something like this.
/* exported myFunction */
This should silence your warning.
Related
Let's say I call window.alert('my message') in dart code. But what if (exactly my case) there is a script which did override alert function before my script. Now if I try to call window.alert('message') It executes overridden alert function instead of initial one.
Is there a mechanism to ensure that all functions are not overridden in described way? Or should I always make sure that dart script is the first to execute on page to ensure that no undesirable overrides are done and behavior is stable and it's the only way to go?
Here is basic github repo if you would like to see the issue live.
EDIT 1:
main.dart
import 'dart:html';
void main() {
window.alert('alert');
}
index.html
<html>
<head>
<title>test</title>
<script>
alert = (variable) => confirm("hehe it's jinxed");
</script>
<script defer src="main.dart.js"></script>
</head>
</html>
That does not sound possible.
If something "overrides" the JavaScript alert function, it actually overwrites the alert property of the window object. The old value is completely lost at that point, and it's not (easily) possible to even detect that this has happened.
If your code doesn't run first in a web browser, you can't protect yourself from this. If your code does run first, you can use Object.freeze or similar functionality to lock down the original properties, or take copies of them into your own variables. Simply running first is not a guarantee, if your code just uses window.alert directly later. You have to make an effort to prevent later code from overwriting it too.
This is really a JavaScript issue, there is nothing special about dart:html or Dart compiled code in this, it affects any JavaScript code which doesn't run first in a browser.
This is the first time I ask a question here. I have the following example case:
The export.js file contains a function and some extra lines of code. In this case, it's just a log:
//exporting.js
function add(a,b) {
return a+b;
}
console.log(add(5,8));
module.exports = add;
The import.js file requires the function add, but when it's executed it also prints the log in exporting.js
//import.js
adds = require('./exporting.js');
console.log(adds(1,2));
So it finally prints:
13
3
I want to know if there's a way to require a function without executing all the required file.
I've tried placing my function inside an object, but it still prints the first log.
I hope you can help me, I haven't find anything related, sorry if it's a noob question.
I want to know if there's a way to require a function without executing all the required file.
No, you cannot. With CommonJS modules, the only way you know what is exported is by running the file and letting it assign things to module.exports. Without running the file, there would be nothing exported.
The design solution for this would be to move the exported function you want into a separate module by itself where that module does nothing except export your function. Then, you can require() that module without any side effects of loading the module.
I am trying to access a variable from another file in JavaScript, and I'm not able to. When I try to print that variable out, for example, Intellisense suggests the variable. However, when I actually run it, I get the error Uncaught ReferenceError: myVariable is not defined
The variable is definitely declared in my file ('homePage.js').
I thought that all variables in Javascript were global, so I'm not sure why this is happening. All my files are in the same folder. Do I need to import something or am I just doing something completely wrong?
Thank you!
Maybe you are trying to access the variable value when your file still not loaded in the browser. You can check this using window.onload
window.onload = function() {
console.log('myVariable', myVariable);
// or execute some function that use the variable
}
https://developer.mozilla.org/es/docs/Web/API/GlobalEventHandlers/onload
You can also move variables into session state / cookie and pull it back into the other script from there.
I am trying to write an app in Unity. I thought that the similarity between the JS and US allows to use JS libraries inside. Seems I am wrong, or I don't get somewhat. So the file is JSmol.min.moJQ.js (it can be downloaded here inside Jmol package, inside jsmol.zip or just the file itself here) and the piece of code which doesn't want to work properly is:
(function(a){
function j(a){
try{return a?new window.ActiveXObject("Microsoft.XMLHTTP"):new window.XMLHttpRequest}
That's not the full code, just first three lines. I've checked the brackets, these seem to be fine.
Now, Unity itself shows me that instead of j (function name) it expected a (.
I have tried to rename the function (just in case) -> Same error.
I have checked this page to learn about differences about US and
JS, but still it shows that the declared functions can be used, but
nothing about using them inside anonymous ones.
The bug page doesn't clarify the problem as well.
This page explains somewhat, at least I think I saw a usage of a declared function inside a declared other one. But again no clue for my case.
What do I miss? How can I fix this?
I'd love to live in a lint free world. JSLint that is. But I'm having some problems that I'm not sure I should ignore or fix. Specifically I'm getting a number of:
[functionName] not defined
errors. I do recognise that you should always define the function before using it and I assume that's what the message is really warning against but here are two cases where I think there is a valid exception:
jQuery(document).ready(function($) {
// code goes here
}
In a simple file which starts with a typical noconflict wrapper for jQuery I get a warning saying that "jQuery(document).ready(function($) {" is not defined. Obviously jQuery is defined in a separate file that needs to stay separate. Does that make sense?
The second example is really quite similar although arguably it is more avoidable. In this case I have two JS files in my project. One that looks just like the one above (aka, it's a set of DOM triggered events wrapped inside of jQuery's "ready" event). The other has a number of helper functions that look like this:
function doSomethingImportant() { };
function doSomethingImpressive() { };
These functions are then called within the first JS file. JSLint doesn't like this and complains every time the first JS file calls a function defined in the second JS file: doSomethingImportant() { is not defined. Yet in reality, since the functions are defined at load time and the calls to those functions always happen afterward based on DOM events there never seems to be a real problem.
Anyway, open to ideas. Is this a case of filtering JSLint's advice or something I should fix in code?
You're getting these errors because JSLint assumes that at some point in the future you might do:
var doSomethingImpressive = function() { }
in which case you would get an error, because the file where the function is defined is included after the function call (as explained here)
The same goes for the jQuery call. You can either change the order of your scripts, or safely ignore the errors.