Checking for a prototype's prototype


(Mark Anderson) #1

This came up in a private discussion but might be of use to others:

If prototype B has a prototype A that it descends from. Does the $Prototype== test pick up that B is A too? Or some other way to do that?

My reply:

$Prototype holds the name of that note’s prototype. There is no ‘prototype’ designator, so we can’t ask for $Prototype(prototype). The following does seem to work (in a quick test as I’ve never had to do this before):

$MyString = $Prototype;
$Text = $Prototype($MyString);
$MyString =;

I did try using a var instead of $MyString but no joy. Then the penny dropped, this does the same thing:

$Text = $Prototype($Prototype);

I’m setting the result in $Text just to check but obviously you’d likely use it in a query. In my test B is ‘Reference’ so I had success with an agent query:

$Prototype($Prototype)=="Reference"

So, that will get you up one level of chained prototype, thus the B in the question above. But, can we go further? Thus I just had to try going one more level:

$Prototype($Prototype($Prototype))=="Dashboard"

Great! I’d be cautious about how far you can go, in terms of this sort of nesting, although the following works:

$Prototype($Prototype($Prototype($Prototype)))=="Event"

For speed I was just using built-in prototypes in a small test file. Thus, I added 4 prototypes: Person, Dashboard, Event and Reference. I gave the ‘Person’ prototype a prototype of ‘Reference’ which in turn had a prototype of ‘Dashboard’ which had a prototype of ‘Event’. My test note used ‘Person’ as its prototype.



In case you’re wondering about the coloured code mark-up above, it is inserted by using three back-ticks on a line of their own immediately before and after your code sample (there is no button in the drafting box). No blank lines are needed before or after the back-ticks to separate them from preceding/following paragraphs.

I’m not not sure what code language is being tested (there’s no specific Tinderbox module for this) but it works well enough. For instance, it is good for spotting this sort of error with quotes:

$MyString = “Hello";

which is better than the normal code mark-up for this sort of edge-case as curly quotes (single or double) won’t be coloured as a string value:

$MyString = “Hello";