What are the variables [[FunctionLocation]], [[Scopes]]: in the Browser Console - ajax

When using jQuery Ajax, in the Browser console I can see the xhr object has two props / fields in some weird notation [double square brackets, don't think it means array in this case];
First, what exactly are they, and second, can I access these values from my JavaScript code?
[[FunctionLocation]]: jquery-3.3.1.min.js:2
[[Scopes]]: Scopes[4]
0: Closure (w.Callbacks) {e: {…}, t: undefined, n: "", r: undefined, i: Array(0), …}
1: Closure {e: Window, r: document, i: ƒ, o: ƒ, a: ƒ, …}
2: Script {loc: Location, baseRestURL: "http://localhost:60123/MyVirtualDir"}
3: Global {parent: Window, opener: null, top: Window, length: 0, frames: Window, …}

No, You can't access them in your code.
The [[FunctionLocation]] property you see in Inspector is added in V8Debugger::internalProperties() in the debugger's C++ code, which uses another C++ function V8Debugger::functionLocation() to gather information about the function. functionLocation() then uses a number of V8-specific C++ APIs such as v8::Function::GetScriptLineNumber() and GetScriptColumnNumber() to find out the exact information.
All APIs described above are exclusively available to C++ code, not JavaScript code. If you are trying to accomplish this task in a platform like Node.js, then you should be able to write a native module. If not, then you are out of luck.
There is another similar question answered here
[[Scopes]] is a private property that Chrome developer tools add and use internally, in C++, here in the source. It displays the variables that are in the scope of a function, i.e. which variables can be accessed from that function.

Related

How to find basic properties of an object, e.g. array, in p5.js?

Incredibly I can't find an answer to this very essential tool to debug.
Say that I have created an index, but I am unsure it properly represents the pixels of a canvas. What command can I run to find summary features of this object, such as: array, integer valued, min, max, length, first 5 - 10 entries etc.
There's no special p5.js support for helping you debug your JavaScript. You can either use an IDE such as Visual Studio Code with browser debugging configured, or you can use the browser's developer tools to inspect local variables or example them via the JavaScript console, having logged them via console.log(obj) as suggested in the comments. In some environments (such as openprocessing.org or editor.p5js.org) the print() function can be used to display debug information, so if you wanted to inspect the contents of an object without using the developer tools you could convert the object to JSON and display it with print(). Example:
let obj;
function setup() {
noCanvas();
obj = {
foo: "bar",
ary: [ 1, 1, 2, 3, 5 ]
};
}
function mouseClicked() {
print(JSON.stringify(obj));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
However, note that in a normal webpage the values passed to print() would only be visible in the browser's JavaScript console. Also not all objects can be converted to JSON strings.

How to reset Firefox console without refreshing the page?

How can I reset a Firefox Web Console in order to be able to use variables already declared?
console.clear() will only clear the output. All variables already declared are sticking in memory. I'm trying to get rid of:
Error: "SyntaxError: redeclaration of..."
When testing something, use the same construction you would (well, should) use to prevent code and variables being dropped in the global scope. Use Immediately Invoked Function Expressions (IIFE) and place your code in it. Each IIFE is it's own context, so re-declaring is not a problem.
(function () {
const myConstant = 1;
console.log(myConstant);
}());
Note that Chrome 80 allows redeclaration of const and let. There exists a relevant bug in bugzilla for Firefox.
if refreshing the page will reset the console, then you can use location.reload() to refresh the page via console.

What is the "=>" operator?

In this link from MDN, it explains how to write a unit test for developing an addon for firefox. However, there are several segments that I don't understand and didn't find any useful result after searching google.
The first one, the following is a paragraph quoted from above link:
In a web page, you can perform Base64 encoding and decoding using the
btoa() and atob() functions. Unfortunately these functions are
attached to the window object: since this object is not available in
your main add-on code, atob() and btoa() aren't available either. So
we'll create a base64 module to expose these functions from the
platform (see Creating Reusable Modules).
What does "the platform" in the above paragraph mean? the "Services.jsm"?
Also in the following code:
const { atob, btoa } = require("resource://gre/modules/Services.jsm");
this makes atob and btoa as one of the Services that is available for other class? or make (constant variables)atob and btoa both reference to the Services.jsm?
The Second one:
what are these two lines of code do?
exports.atob = a => atob(a);
exports.btoa = b => btoa(b);
I understand the part
exports.atob
which enables atob function to be available from other classes outside the "base64.js".
but what does the following mean?
= a => atob(a);
I didn't find that javascript has "=>" operator!
From my understaning, the reason we create a base64.js is because atob and btoa can only be called if we have the window object. Therefore we can use Services.jsm to get a reference of the window object?
For "platform" in the paragraph you quoted they mean the set of functions which is not strictly Firefox browser code but rather implements basic, share functionalities. This usually lives in toolkit/modules in the mozilla-central repository. Services.jsm lives there as well so yes, that's part of the platform. Moreover, atob and btoa are both imported from Services.jsm.
The arrow => in exports.atob = a => atob(a); defines what's called an arrow function: it's a new, shorter syntax to define functions in JavaScript. This SO answer has many useful informations about it.
From my understanding, the reason we create a base64.js is because atob and btoa can only be called if we have the window object. Therefore we can use Services.jsm to get a reference of the window object?
That's almost correct: you need to export the function from the underlying platform as you don't have a window object there. If you had a window object, you would have just done window.atob or something like that. That call would have still called the same function you imported from Services.jsm.
So you're not using Services.jsm to get a reference to the window object, but rather directly importing the needed functions so that you don't need to have a window object.

How to use NSMaxXEdge in Cocoa app with Swift (Xcode 6)?

I've tried to create a Cocoa app which uses NSPopover internally. One of the popover's method is showRelativeToRect: ofView: preferredEdge:, which triggers an popover in Cocoa app.
If you use Objective-C in Xcode 5.1, you can execute the method with the method above, like this:
[popover showRelativeToRect: sender.bounds ofView: sender preferredEdge: NSMaxXEdge];
However, when I tried to use the method in Swift in Xcode 6, the message Use of unresolved identifier 'NSMaxXEdge' was shown when I wrote the following method, which is just a rewrite of the Objective-C method above:
popover.showRelativeToRect(sender.bounds, ofView: sender, preferredEdge: NSMaxXEdge
So where is the NSMaxXEdge gone? The official documentation says it is of type NSRectEdge, but what is the NSRectEdge? The doc doesn't link to NSRectEdge page.
Also, Xcode 5 documentation also says it is NSRectEdge, but again, no link exists there. So how can I know about what it is all about?
And finally, if NSMaxXEdge is no longer available in Xcode 6, what is the alternative?
In Foundation/NSGeometary.h of 10.10 SDK:
typedef enum {
NSMinXEdge = 0,
NSMinYEdge = 1,
NSMaxXEdge = 2,
NSMaxYEdge = 3
} NSRectEdge;
So it should be NSRectEdge.from(2) or NSRectEdge(2) or just pass 2 will be fine.
If you try this:
println("NSRectEdge.max: \(NSRectEdge.max)") // 9223372036854775807
println("NSRectEdge.from(2): \(NSRectEdge.from(2))") // 2
println("NSRectEdge(2): \(NSRectEdge(2))") //2
You will know that .max was actually the max positive signed Int for 64-bit.
(Tho, I can't be sure since it's not really mentioned in documentation anywhere.)
NSRectEdge has a static var called max. I think you should just use that. For example:
popover.showRelativeToRect(rect, ofView: view, preferredEdge: NSRectEdge.max)
(The below is true for beta 5 (developing for 10.9). Your mileage may vary.)
Above, you get a solution that works - passing 0-3 where the function calls for a NSRectEdge. I'd like to pick up on
how can I know about what it is all about?
The documentation indicates that NSRectEdge is bridged with CGRectEdge, but trying to pass in CGRectEdge led to an error of
CGRectEdge is not convertible to NSRectEdge
NSRectEdge.from(2) or NSRectEdge(2) - as suggested above - come back with the error that
NSRectEdge has no accessible initializers
When you type NSRectEdge into Xcode, however, it shows two types:
CGRectEdge (which in this case is useless, see above) and Int.
I'm afraid that you need to guess that NSRectEdge is an enum when you see it both called as a string and defined as an int; but from there to looking up its definition in the framework is a logical step; as is guessing that you will have four values, one for each edge.
Incidentally, there seems to be an internal modulo operation going on somewhere - when you pass it '4', you get NSMinXEdge again and so on.

Logging an onFailure inError in WebOS

An onFailure handler in webOS has an argument inError. I tried printing it using: console.log("error: " + inError);, but the result is only: error: [object Object]*** (app/assistants/main-assistant.js:26), which isn't much use. How can I log something more useful instead?
Update:
Ares generates: alarm1Failure: function(inSender, inError) {}. However, the error is contained as the errorText property of the first object and the second object is the request
I would use the interactive debugger at:
http://ares.palm.com/AresDebug/
Connect your device and run your app. Put your app name in the 'Script Filter' box and click get scripts.
Now use the 'Available Scripts' pull down to find your assembly.
You can set breakpoints (click on line numbers to the left) and inspect variables using the lower left pane and '>' prompt.
Be sure to use Chrome or Safari as it will not work with IE.
There is also a logger at:
http://ares.palm.com/AresLog/
if you don't want to use the debugger, then you probably should know something about the inError object getting returned to you. In this case I assume the onFailure comes from a Protocol function's callback, so try looking in the Protocol documentation to see what information the error object should contain.
Also for any logging purposes don't forget about the imensely useful function
JSON.stringify(obj)
It will take an object and return a JSON representation that you can log so you can see all the properties at once.

Resources