Javascript "Graph" object? - javascript

I wanted to toy around with A* for the purposes of game making. So I found this little javascript library: https://github.com/bgrins/javascript-astar
This is what the basic usage looks like:
var graph = new Graph([
[1,1,1,1],
[0,1,1,0],
[0,0,1,1]
]);
var start = graph.grid[0][0];
var end = graph.grid[1][2];
var result = astar.search(graph, start, end);
I digress; I've never seen a "Graph" object in Javascript before. I've tried Googling it, and I haven't found anything. I haven't found anything here either. (Maybe I just don't know the right questions to ask?) I looked through the code for the library, and I don't see anything indicating that it's proprietary to the library. It doesn't look like I need to include any other libraries either.
Does anybody know what this is? Is this a native javascript object type? Is it basically the same thing as a 2D array (which is what I've been using so far)?
As always, pointers and advice are always appreciated.

Related

customizing firefox console.log (for my own prototype)

I'm coding a (free software) application (the MELT monitor, on GNU/Linux/Debian/x86-64) which embeds its specific web server.
See this question for gory details. If interested, look into commit 880419d370d749 on github then build it, run ./monimelt -Dweb,run -W localhost.localdomain:8086/ and open http://localhost.localdomain:8086/canvedit.html in your Firefox. FWIW I'm now trying to use canvases. Relevant code might go in my webroot/canvasedit.js, but I don't know what to code there yet...
Firefox is version 38 or 42. I don't understand all the details of ConsoleAPI.js which probably is very relevant to my question.
Let suppose I have a prototype
var fooproto = {
// perhaps adding a toString function is enough here?
// I want to show num & sons
};
then I make some objects using it:
var foo1 = Object();
foo1.num = 11;
foo1.__proto__ = fooproto;
var foo2 = Object();
foo2.num = 37;
foo2.sons = [foo1];
foo2.__proto__ = fooproto;
I would like
console.log("foo2=", foo2);
to show something like foo2=Foo#37[Foo#11] on the console (if possible with the italics)
Is there a way to change fooproto to make that work? I guess that some mechanism exist, since console.log is displaying nicely DOM objects.
It is probably a FAQ asked many times, but I was not able after several minutes of searching to find the appropriate search keywords. I don't know what terminology to use
I’m not developing for Firefox and apologies if you’ve already seen this, but I’ve stumbled across this:
Custom output in the Web Console on MDN, and specifically console API
Seems like something that might answer your needs.

Can you instantiate an Element object AND define attributes simultaneously?

This may seem like a silly question to some. The short background is; I am clinically diagnosed with OCD, I am thus very particular about the formatting and neatness of my code so I apologize in advance. This leads me to my question:
Is there a way in javascript to instantiate an object and define its attributes in a block? My goal might be somewhat analogous to how you'd see a JSON object/string
Here's some pseudocode/formatting:
var preElement = document.createElement('pre')
.className = "nodeResults";
.innerHTML = formattedResponse;
.style = "blahblah";
.anymoreAttributes = "stuff";
Inconsequential, I know. I just noticed I spent 30 minutes researching this instead of writing functional code. Downfall of OCD. SO please help me; Is this possible yes, or no?
Sincerely,
WastingTehTime
This is not doable unless you write a class wrapper to handle this kind of formatting, or use the jQuery library.
jQuery example:
var preElement = $(document.createElement('pre'))
.addClass("nodeResult")
.html(formattedResponse)
.css(jsonFormattedCSS)
jsFiddle example: http://jsfiddle.net/3h5kfv2j/
This can be implemented in vanilla javascript too, but you will need to find a library that does this or code it yourself.
Here is some sample code of a vanilla implementation I just made: http://jsfiddle.net/4n4w3uqr/

Strange javascript behaviour - error unless 'classes' are defined in correct order

I have a very strange problem with javascript and easel js.
I am using the easel.js library and am already fairly far into the construction of a project using it.
I am attempting to have a 'class' (I know they aren't technically classes in javascript but I will use this terminology for lack of a better word) inherit the Shape class from easel js, and then have another class inherit that. So it would be something like this:
easeljs.Shape --> MenuButton --> BuildingButton
The code I am using looks like this:
BuildingButton.prototype = Object.create(MenuButton.prototype);
BuildingButton.prototype.constructor = BuildingButton;
function BuildingButton(){
MenuButton.call(this);
}
MenuButton.prototype = Object.create(createjs.Shape.prototype);
MenuButton.prototype.constructor = MenuButton;
function MenuButton(){
createjs.Shape.call(this);
}
The problem is that I get the following error with this code:
Uncaught TypeError: undefined is not a function
easeljs-0.7.1.combined.js:8439
(line 8439 is pointing to the initialize() function in the Shape() constructor).
now here's the strange thing. If I change the order of the definitions so that the sub class is defined second and not first, it works fine!
MenuButton.prototype = Object.create(createjs.Shape.prototype);
MenuButton.prototype.constructor = MenuButton;
function MenuButton(){
createjs.Shape.call(this);
}
BuildingButton.prototype = Object.create(MenuButton.prototype);
BuildingButton.prototype.constructor = BuildingButton;
function BuildingButton(){
MenuButton.call(this);
}
This is very confusing as I can't seem to figure out why on earth this is happening. I could just make sure I define them in the correct order and leave it be, but I have all my 'classes' in different source files which are then strung together by grunt, which does so alphabetically.
Also, I feel like I may have a big gap in my knowledge of javascript (or maybe easel.js I'm not sure what exactly is causing this behaviour).
Thanks in advance for your help and I hope the question makes sense!
MenuButton.prototype = Object.create(createjs.Shape.prototype);
…
BuildingButton.prototype = Object.create(MenuButton.prototype);
These two statements have a clear dependency and need to be executed in the correct order (for the function declarations the order is irrelevant if placed in the same scope/file, but if in different files they need to be loaded in the correct order obviously).
I have all my 'classes' in different source files which are then strung together by grunt, which does so alphabetically
That's not a good idea. You should use some build tool/script that allows the declaration of dependencies.
Read this to clear things out: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain
In first example you try to inherit from nothing, since MenuButton.prototype is not yet defined. To make it work just add MenuButton.prototype = new createjs.Shape.prototype(instead of Object.create() wich shouldn't be used anymore) to instantiate it first before you can you use it. Your first code is like you are willing to eat a banana before having one.

Debugging dynamically generated/loaded JavaScript code INSIDE the host JavaScript

The title probably isn't saying much at all, but hopefully it got someone interested.
The actual problem is quite simple.
I want to make a web application that loads JavaScript code dynamically (might even be a hardcoded string) as a string, then I want to run that code line by line and analyze the debug info every step.
Basically, I need a JavaScript interpreter/debugger written in JavaScript.
Hopefully, I'd be able to do something like this
var source = "var a = 5; var b = a; b = 6"
var vm = new VirtualMachine(source);
vm.step();
vm.getDebugInfo();
and it would tell me about the local variables and their values.
This project hasn't been updated in a while but maybe you could build something for your purpose around it
https://github.com/jterrace/js.js/ - A JavaScript JavaScript interpreter

How to get javascript new object information from javascript from webkit?

I want to extract a new objects information name and arguments into a HTML page.
Such as
<script>
var a = new g(10,20);
</script>
I need to print :
new object g with arguments 10,20
I am newbie to Webkit.
Initially I thought just add a printf statement in the javascriptcore. I added some printf statements to
JavaScriptCore/runtime/FunctionConstructor.cpp ,ObjectConstructor.cpp .
But didn't get right result .
I googled but there is few tutorials about javascriptcore.
I hope some expert can point me in the right direction .
I'm not sure which version of WebKit are you hacking with, so my solution may not work.
I suggest you to add logging statements in Interpreter.cpp, inside the block of DEFINE_OPCODE(op_construct).

Categories

Resources