Join Bot Libre's Partner and Affiliate program and support our free open platform

How is Self different from JavaScript and JSON?

by admin posted Apr 1, 13:47

Bot Libre's scripting language Self is modeled after JavaScript, but is not JavaScript.

The syntax of Self is very similar to JavaScript, but it does have some different operators and behavior. One of these difference is related to Bot Libre's data model, which is similar to JavaScript's data model but does have some differences. These differences arise because Bot Libre's data model is designed specifically for artificial intelligence and allowing data to be represented similar to how the human brain works.

In JavaScript you can create a new object and assign a variable such as:

var Person = function() { };
var person = new Person(); = "William"; == "William"

In JavaScript the last line returns true.

In Self you do not need to declare classes, you can just use new. But everything else would be the same.

var person = new Person(); = "William"; == "William"

In Self the last line is also true.

How Self is different is that object variable can be assign multiple values. =+ "Will"; == "Will" == "William"

The =+ operator is specific to Self, and allows you to assign addition values to a variable. In the above the person's name would be both Will and William. This is similar to the human brain, if I introduce myself as William, but them someone calls me Will, you would be okay with knowing my name is both Will and William.

Self can also have negative values. =- "Wally"; == "Wally"

The last line will return false.

Self also understands the difference between knowing something is wrong, and not knowing something is right. In fact all relationships track a "correctness" decimal value between -1.0 and +1.0 which defines how certain the bot is about the relationship. == "Wilbur";

The last line will return #unknown. Boolean values can be the symbols #true, #false, and #unknown.

In Self because object variables can have multiple values the array notation can be used to access them.[0] == "William"[1] == "Will"

This can create some confusion when dealing with objects that have arrays, and in this case the Self syntax differs from the JavaScript syntax.

var person = { "name" : "Will", "children" : [ { "name" : "Will Jr." }, { "name" : "John" } ] };
person.children[0].name != "Will Jr."

Here children[0] means the first value of the children relationship, not the first value in the children array.

For arrays you can use the "elements" variable to traverse into the array elements.

person.children.elements[0].name == "Will Jr."

You could also use a variable to avoid the confusion. In general it is better to use variables when dealing with array to avoid confusion.

var children = person.children;
children[0].name == "Will Jr."

Here, because a variable was used, this is true.

Id: 42565876
Tags: javascript, self, json
Posted: Apr 1, 13:47
Updated: Apr 1, 13:50
Replies: 0
Views: 254, today: 2, week: 2, month: 23
1 0 3.0/5