[JavaScript Challenge] Figure this out

Joined
Jun 8, 2007
Messages
1,965
Reaction score
482
Here's a challenge for you:

Figure out what this library does:

(Library: )
PHP:
var waject = function(o, Preset, Got) {
	"use strict";
	o = Object.create(o || null);
	var item, Main = {}, $ = function (property, value) {
		Object.defineProperty(Main, property, {value: value});
	};
	$('mk', function (props, val, preset, got) {
		var nil = function () {}, prop = (
			props.property || props
		);
		Object.defineProperty(Main, prop, {
			get: function () {
				if ((props.got
				 || got
				 || Got
				 || nil
				)(o, prop) !== false) {
					return o[prop];
				}
			},
			set: function (value) {
				if((props.preset
				 || preset
				 || Preset
				 || nil
				)(o, prop, value) !== false) {
					return o[prop] = value;
				}
			},
			configurable: true,
			enumerable: true
		});
		o[prop] = (props.value || val || null);
	});
	$('toString', function () {
		return JSON.stringify(o, null, '\t');
	});
	for (item in o) {
		if (typeof o[item] === "object") {
			Main.mk(item, waject(o[item], Preset, Got));
		} else {
			Main.mk(item, o[item]);
		}
	}
	o.toString = Main.toString;
	return Main;
};

. . .

(Implementation: )
PHP:
(function (waject) {
	"use strict";
	var o = waject({
		a: 1, 
		b: 2, 
		c: {
			d: 3, 
			e: 4
		}
	}, function (obj, property, val) {
		console.log(property
			+ " change from "
			+ obj[property]
			+ " to: "
			+ val
		);
	});
	o.c.d += o.a * o.b;
	console.log("o: " + o);
}(waject));

Console Output:
Code:
d change from 3 to: 5
o: {
	"a": 1,
	"b": 2,
	"c": {
		"d": 5,
		"e": 4
	}
}


(Note: You can try this in a good, modern browser that supports console.log for development purposes, by surrounding the code in "<script> .. </script>" tags.)

Kudos to people who can figure it out, and these are released under the BSD license- enjoy.
 
Last edited:
Is this some Minecraft mod? Lmao

Lol no, I'd call it an Evented JavaScript Object Model- general purpose.

But the question remains, what's it do? What is a "Waject" in respect to a regular Object?

Another implementation example:
PHP:
	var o = waject();
	o.mk('foo', 'bar', function(value) {
		console.log("Foo was set!");
	}, function() { 
		console.log("Foo was accessed!")
	});
	
	console.log(o.foo);
	//Foo was accessed!
	//bar
	
	o.mk({
		property: 'baz',
		value: 'bam!',
		preset: function(value)
		{
			console.log("Overwriting baz!")
		}
	});
	
	o.baz = "boom!";
	//Overwriting baz!
	console.log("o:" + o);// foo is "bar" and baz is "boom!"

And a more in-depth example to give away the answer:
PHP:
(function (waject) {
	"use strict";
	var o = waject();
	o.mk('foo', 'bar', function(value) {
		console.log("Foo was set!");
		
	}, function() { 
		console.log("Foo was denied accessed!");
		return false;
	});
	
	console.log(o.foo);
	//Foo was denied accessed!
	//undefined
	
	o.mk({
		property: 'baz',
		value: 'bam!',
		preset: function(value)
		{
			console.log("Blocking baz Overwrite!")
			return false; //return false to cancel set.
		}
	});
	
	o.baz = "boom!";
	//Blocking Baz Overwrite!
	console.log("o:" + o);// foo is "bar" and baz is "bam!"
	
}(waject));
 
Last edited:
My javascript is somewhat weak - I am not all that well versed in it. But, it looks like some sort of JSON debugger.
Place the JSON array in the waject function, and then, from what I can tell, the console.log functionality changes to a custom, more JSON-specific debugger, if you will.

But, between the massive nests of callbacks and the implementation wherein I am not sure which parts of that are and aren't normally possible without your library... it is difficult for me to specify a complete and valid purpose.
 
My javascript is somewhat weak - I am not all that well versed in it. But, it looks like some sort of JSON debugger.
Place the JSON array in the waject function, and then, from what I can tell, the console.log functionality changes to a custom, more JSON-specific debugger, if you will.

But, between the massive nests of callbacks and the implementation wherein I am not sure which parts of that are and aren't normally possible without your library... it is difficult for me to specify a complete and valid purpose.

It takes any JavaScript object- you noticed the toString() method displays friendly JSON, but you can still get the raw Object, too. There's a bigger purpose beyond debugging. Find it :D:

Don't pay attention to the low-level library end, pay attention to what you can do with Wajects that you cannot do very easily with normal Objects.. Well, the Waject library makes something new possible with Objects, which is normally only possible with properties.

PHP:
	var squeak = function(o, p) { console.log(p + '->SQUEAK!'); },
	o = waject(
		{a: 1, b: 2, c: {d: 3}},
		squeak, 
		squeak
	);
	o.c.d += o.a * o.b; //produces a bunch of squeaks!!??

Produces:
Code:
c->SQUEAK!
d->SQUEAK!
a->SQUEAK!
b->SQUEAK!
d->SQUEAK!

Why did all of the properties squeak from one line of code?

I promise, there's something fundamental here.
 
Last edited:
It takes any JavaScript object- you noticed the toString() method displays friendly JSON, but you can still get the raw Object, too. There's a bigger purpose beyond debugging. Find it :D:

Don't pay attention to the low-level library end, pay attention to what you can do with Wajects that you cannot do very easily with normal Objects.. Well, the Waject library makes something new possible with Objects, which is normally only possible with properties.

PHP:
	var squeak = function(o, p) { console.log(p + '->SQUEAK!'); },
	o = waject(
		{a: 1, b: 2, c: {d: 3}},
		squeak, 
		squeak
	);
	o.c.d += o.a * o.b; //produces a bunch of squeaks!!??

Produces:
Code:
c->SQUEAK!
d->SQUEAK!
a->SQUEAK!
b->SQUEAK!
d->SQUEAK!

Why did all of the properties squeak from one line of code?

I promise, there's something fundamental here.

Hmm... I'll give it another shot then.

This library allows the manipulation of the output of a JSON array of objects which is converted to a JSON array of strings (and thus the strings may or may not have been objects that were converted to strings).

The squeaking happens because you are outputting the property (p) without the value (v). Thus the value is ignored and only the property is outputted followed by the string "->SQUEAK!".

The order of the output is a tad more puzzling to me. cdabd. I know why it outputted the first 4. That was the order of the calls in the equation. I am not all that sure as to why the property d was outputted after all that. Perhaps it has to do with the fact that it is nested in the c property.

Once again, my javascript knowledge is limited, so don't pester me if I am completely off :3.

P.S. o.c.d I didn't miss that ;).
 
It takes a given object and any child objects and wraps their properties in getters and setters that call a provided callback, and depending on that callback's return value it will permit the get/set or reject it, defaulting to rejecting. Preset is called in the setter and Got in the getter. That Preset/Got function can either be the global one or one provided in the object itself.

Seems overly clever to be useful. Manually defining getters/setters tends to be a lot clearer, so I don't see something like this in use outside of library internals for brevity. And you can't really get full per-property granularity here.
 
Last edited:
Hmm... I'll give it another shot then.

This library allows the manipulation of the output of a JSON array of objects which is converted to a JSON array of strings (and thus the strings may or may not have been objects that were converted to strings).

The squeaking happens because you are outputting the property (p) without the value (v). Thus the value is ignored and only the property is outputted followed by the string "->SQUEAK!".

The order of the output is a tad more puzzling to me. cdabd. I know why it outputted the first 4. That was the order of the calls in the equation. I am not all that sure as to why the property d was outputted after all that. Perhaps it has to do with the fact that it is nested in the c property.

Once again, my javascript knowledge is limited, so don't pester me if I am completely off :3.

P.S. o.c.d I didn't miss that ;).

The last 'd' is from the setter, all of the other ones are from the getters.

Good 'nuff.


jMerliN got it right, but instead of 'global' there's a better wording somewhere.. 'global' may be confused with the global scope- which is bad and inaccurate.


Basically, you can attach an event on any property within an object. If that property is an object, then you can call waject() to add getters/setters all of the inner properties (recursively- NO CIRCULARS yet).

Basically, you can code a system where, if 'e' changes, it effects 'm' and 'c' automatically which in-turn can effect 'e' again in order to accommodate for 'e=mc^2'.

There are many-a-library which can use this. In any case, I'm still trying to figure it out. There is a fundamental about evented programming I'm just beginning to understand.

the term I was looking for was actually "evented." This isn't the kind of event pre-defined by w3c or microsoft for use with visual document models, these events are triggered when data is touched or changed. You can cancel reads/changes in the event.

It simply goes a step or three further than Mozilla's Object.walk() or jQuery's $.watch() methods. Now, I admittedly never found a use for those, but I've found a use for an easier interface for watching objects with reconfigurable bubbling getter/setter properties - or wajects.

What will I make? Probably an event-driven library for Node.js... Most-likely for data-bindings.
 
Last edited:
Back