Can a declared function be inside the anonymous one in UnityScript? - javascript

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?

Related

Chrome console - breakpoint over whole file

is there any option to set something like "breakpoint" on a file in chrome console (kindof shortcut to set breakpoint on every line of code in the file)?
Would be extremely useful when trying to understand 3rd party scripts that you know are executed but have no idea which part of code and from where is executed when.
My current example use case: I downloaded a script (form validation) which does not work as expected. The fastest way to solve the problem would be to pause execution anytime JS runtime enters this file and start exploring it from there.
I think this will be of use to you. I've recently been doing some work on the JavaScript Breakpoint Collection Chrome Extension created by Matt Zeunert, which allows you to inject breakpoints into your code at runtime - including breaking on property access/modifications, functions, scrolling events, etc. You can break on any arbitrary objects as well as the predefined ones using the console API.
Check out the project here.
If you can enumerate the functions publicly exposed by your third party script (for example if they are all properties of an object, or is their name has a pattern) you can make another script which dynamically replaces all those functions and force a break point :
thirdpartfunc = (function () {
var oldfunc = thirdpartfunc;
return function () {
debugger;
oldfunc.call(null, arguments);
}());
With the appropriate binding to this (if any applicable).
If you know the function(s) being called, you can use function breakpoints
debug(function);
function(...args);
When you call the function, it will hit the breakpoint. These aren't saved on page reload, but you can set a line breakpoint once you hit the function breakpoint.
This can get kinda tedious though.
If you have a array of functions, you can do
[function0, function1].map(debug)
#Tibos answer in another post would be good if there was some sort of babel transform to insert debugger; at the start of every function, instead of inserting it manually.
The quickest way for me was to do a global replace of the function declarations in the source, adding "debugger;" at the start.
In other words, replace
function [^{]*{
with
$0 debugger;
Your regexp may vary depending on the escapes you need. For example:
function [^{]*\{
You may also need more than one pattern to handle all the function declarations you have.
This has nothing to do with the Chrome console though.
No. You would have to add breakpoints to the various function entry points that file contains to catch everywhere it could enter.
Can't you just place a breakpoint at the first line of every function in the file of interest?

Export Flash parameters to JavaScript

I'm trying to pass parameters from Flash (as 3.0) to JavaScript.
Tried all methods I found in via. Google, as:
ExternalInterface.addCallback ("fonts", recieveFromJS);
Always one and the same problem; when I try to call the fonts () swfobject, JavaScript gives the error that the method doesn't exist.
Assuming your javascript code does not have a syntax error somewhere, this usually happens because of jquery (or some other js bundle) is stepping on your code. Try using a test page with just the javascript you need, removing all other code and header entries. If it works, then add scripts and links back, one at a time, and you will find which code is breaking it. If it does not work even in your test page, then you have a code/syntax/logic problem with the snippet of code you are working with. If you still have a problem with a code snippet, post it here and I or someone will surely help debug it for you.

JSLint and undefined functions

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.

Aptana - Open Declaration

So I downloaded Aptana because many people have said it is the best IDE for javascript. Grabbed a project off github, went to a line in the code that was a method call, right clicked on it and noticed 'open declaration - F3'. Tried both it and F3 with absolutely no response from Aptana....Aptana uninstalled.
Out of curiosity is there any way in Aptana to go from a method call or variable to its declaration? If something that is version 3 cannot do this out-of-the-box, makes me wonder why the hype?
I too recently tried out version 3 after being dissapointed with v2 a while ago.
Open declaration works, if the method is in the same file.. otherwise it fails across multiple files in the same project.
That concludes the answer portion, rest is just my angry rant:
What is even worse that with HOVER on the function call it actually shows the location where the function is located, that is it shows exact directory and file name, yet F3 or open declaration does nothing as OP said.
This means, that there has to be some way of triggering the function lookup across other files in the same project.
How can it be that Aptana knows where the function is located, yet is unable to open it within the same project?
Where are you APTANA developers?
Actually, you can do this though it's not perfect. If you see that it's finding a path to the function, ctrl+hover over the function and it should pop down a list of paths to places where the function was defined in your workspace. Click on one and it will jump you to that declaration.

Including Prototype.js twice causes conflict in IE7

I am using the Prototype javascript framework. I have included it in a javascript code snippet that I allow people to copy and paste onto their websites. There is a possibility that their website already includes Prototype or they wanted to include the snippet multiple times. In both cases, Prototype will be defined twice and, as a result, IE7 will not function. It will say "Object does not support this property or method" on nearly any Prototype library function call. I tried this:
if (typeof(Prototype) === 'undefined') {
alert('including Prototype');
// minified Prototype code here
}
alert('running my code');
// all my code here
When I ran this, only "including Prototype" was alerted, but "running my code" never got alerted. Why?
only "including Prototype" was alerted, but "running my code" never got alerted. Why?
I don't know without seeing the code—do you get anything in the JavaScript error console?—but one possibility is that something in the code relied on being in an unadorned global scope. For example the function statement may not be used inside an if in standard ECMAScript. What actually happens if you try is browser-dependent, but typically it may override a previously-declared version of the function even if the if clause does not evaluate true.
This problem could be solved by putting Prototype in its own <script> block that's inserted into the page via DOM methods or document.write before the main script executes. However:
I have included it in a javascript code snippet that I allow people to copy and paste onto their websites.
Yeah, I really wouldn't do that. Using more than one wide-ranging, intrusive framework like Prototype or jQuery on a single page is a recipe for conflict and potentially hard-to-debug problems. (Same with two copies/versions of the same framework.)
When you are a pastable snippet you are a guest in another webmaster's house need to have as low a footprint as possible. IMO that means you should not use any framework.

Categories