"this" local variable for Snap! lists

I have noticed that there seems to be no Snap! equivalent of Javascript (and presumably many other languages)'s this variable, that is used to reference the parent array that a child item is in. This makes it difficult or neigh impossible to do something such as storing a script in a list that, when called, modifies said list.

As I understand it, JS this isn’t about arrays, but about objects. An array in JS, like a Snap! list, is a collection of independent items. The same item might be a member of several different arrays. It would be a violation of the principle of first class data if a list item were somehow hardwired into a particular list.

As of v10, Snap! lets you use a dictionary, a/k/a an association list, i.e. a list of name-value pairs, as an object. Each name-value pair is a field of the object. The OOP library provides an inheritance mechanism by including a field named in each object whose value is another object, from which this object inherits fields. (The name is rather than, say, parent, because there’s no mechanism in Snap! for translating user data, and the teachers who wanted this feature are German. :~) But, since Snap! itself is translated into different (human) languages, you can say

untitled script pic (7)

to get the object’s parent, i.e., its field.

If you’re using lists as objects in this way, then it does make sense to want a handle on “this object” and indeed Snap! lets you do so, by using… this!

untitled script pic (8)

This is perfect, thank you!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.