Help me understand some javascript

Status
Not open for further replies.
I've been looking at this javascript implementation of Fortune's algorithm to calculate Voronoi diagrams:
http://www.raymondhill.net/voronoi/rhil ... -demo1.php

I'm trying to wrap my mind around the implementation, which is available on Github here. It has no dependencies, supposedly.
https://github.com/gorhill/Javascript-V ... oi-core.js

I understand object-oriented programming concepts, but I'm not familiar with implementing them in javascript and I'm not well versed with js, so I'm trying to work out what's happening.

Around line 189 is the following:

Code:
Voronoi.prototype.RBTree = function() {
	this.root = null;
};

root is never declared as a function or variable, though it is part of a tree structure. How can it be used without being declared?
 
ClayH":3lstkk9h said:
Well, like I said, I'm not very familiar with javascript. Is it common practice to do that or are variables usually declared first?


Basically, it's a way to shoehorn OOP into JS. It's fairly common to see code like this:

Code:
function MyClass() {
    var self = this; // reference to parent for children
	
    this.Init = function () {
    	// constructor for member vars
    	self.someMember = blah;
    	self.someOtherMember = blahblah;
	};
	
    this.SomeMethod = function(someParam) {
        this.localVar = someParam.doSomething();
        return self.SomeHelper(localVar);
    };

    this.SomeHelper = function(someParam) {
        this.retVal = // do stuff with someParam;
        return retVal;
    };

    this.init(); // force constructor init
}

Now you can use that function like a class:

Code:
var myClassInstance = new MyClass();

myClassInstance.SomeMethod(someVal);

Does that clarify a little?
 
Javascript has OOP, but it is based on the notion of prototypes; if you expect it to behave like Java/C#/probably a whole bunch of other languages, you are going to walk away cursing Brendan Eich's name and everyone who contributes to the insanity after a pretty short while.
Thankfully, there's no shortage of pages that try to explain the prototype object model out there.
 
Thanks, AnObfuscator - it does help explain. The notion of prototypes threw me for a loop at first, but more than anything, I was just surprised to see something undeclared that wasn't throwing an error.

The particular library that I referenced has a lot of code like this:

Code:
Voronoi.prototype.RBTree.prototype.rbRotateRight = function(node) {
	var p = node,
		q = node.rbLeft, // can't be null
		parent = p.rbParent;
	if (parent) {
		if (parent.rbLeft === p) {
			parent.rbLeft = q;
			}
		else {
			parent.rbRight = q;
			}
		}
	else {
		this.root = q;
		}
	q.rbParent = parent;
	p.rbParent = q;
	p.rbLeft = q.rbRight;
	if (p.rbLeft) {
		p.rbLeft.rbParent = p;
		}
	q.rbRight = p;
};

Some variables are declared in the library and some aren't; that confused me.
 
Just think of every object as a mutable key/value map. Saying x.prop = val is like putting the pair (prop, val) into the map x. It doesn't require there to already be a x.prop property. Properties can be set, updated, and deleted at any time, just as with any mutable map.

It's kind of unsettling that the only way to get a handle on what properties an object has is to search through the source code (or use a debugger), unlike say Java where you just have to look at the fields of the class (and superclasses). The unstructured nature of JS makes it nice for quick scripting, but doesn't scale so well to complex software.
 
Solomonoff's Secret":1ltx8r7y said:
The unstructured nature of JS makes it nice for quick scripting, but doesn't scale so well to complex software.

Yeah, JS is very "expressive", which means you get very, uh, "personalized" code... to put it nicely. ;) Fortunately, many larger JS projects try to adhere to conventions, which mitigates this somewhat.
 
Status
Not open for further replies.