1 Server side script Objects
An object is a collection of related data that usually consists of some variables and functions, which are called properties and methods when they are inside objects. It is used to store a collection of defined data and more complex entities.
Properties
are those that express a quality of the object, while methods
are functionalities, that is, functions or modes of operation associated with an object.
<xsql-script> <body> <set name='f_name'>Carlos</set> <set name='l_name'>González</set> <set name='code'>25701</set> <set name='country'>España</set> <set name='city'>Barcelona</set> <struct.declare type='s_user'> <field name='firstname' type='string' /> <field name='lastname' type='string' /> <field name='code' type='integer' /> <field name='country' type='string' /> <field name='city' type='string' /> </struct.declare> <set name='m_user' type='struct'> <struct type='s_user'> <string><f_name /></string> <string><l_name /></string> <number><code /></number> <string><country /></string> <string><city /></string> </struct> </set> <println><m_user /></println> </body> </xsql-script>
struct.s_user
{
s_user.firstname (1 - CHAR java.lang.String) = Carlos
s_user.lastname (1 - CHAR java.lang.String) = González
s_user.code (4 - INTEGER java.lang.Integer) = 25701
s_user.country (1 - CHAR java.lang.String) = España
s_user.city (1 - CHAR java.lang.String) = Barcelona
}
var user = { firstName: 'Carlos', lastName: 'Gonzalez', code: 25701, country: 'España', city: 'Barcelona' }; console.log(user);
{
"firstName": "Carlos",
"lastName": "González",
"code": 25701,
"country": "España",
"city": "Barcelona"
}
1.1 Object properties
An object is created with an optional list of properties between "key-value", where the name of the property is always a string and its value can be any type of data, such as strings, numbers, booleans or complex data types like functions and other objects.
Properties whit functions
Properties with functions as their values are called methods
to distinguish them from other properties.
To access a property you must use a valid variable name to extract directly to the property named by the value stored in the variable. Otherwise the code will return an error message indicating that the property is undefined
.
undefined field 'variable' on 'object'
An example of syntax to access the properties of an object is:
<xsql-script> <body> <set name='f_name'>Carlos</set> <set name='l_name'>González</set> <set name='code'>25701</set> <set name='country'>España</set> <set name='city'>Barcelona</set> <struct.declare type='s_user'> <field name='firstname' type='string' /> <field name='lastname' type='string' /> <field name='code' type='integer' /> <field name='country' type='string' /> <field name='city' type='string' /> </struct.declare> <set name='m_user' type='struct'> <struct type='s_user'> <string><f_name /></string> <string><l_name /></string> <number><code /></number> <string><country /></string> <string><city /></string> </struct> </set> <println>He is <m_user.firstname /> <m_user.lastname /> and he lives in <m_user.city />, <m_user.country />.</println> </body> </xsql-script>
He is Carlos González and he lives in Barcelona, España.
var user = { firstName: 'Carlos', lastName: 'González', code: 25701, country: 'España', city: 'Barcelona' }; console.log('He is ' + user.firstName, user.lastName + ' and he lives in ' + user.city +', ' + user.country +'.');
He is Carlos González and he lives in Barcelona, España.
1.1.1 Accessing Properties
The syntax for accessing the property of an object is:
<xsql-script> <body> <struct.declare type='s_user'> <field name='firstname' type='string' /> <field name='lastname' type='string' /> <field name='code' type='integer' /> <field name='country' type='string' /> <field name='city' type='string' /> </struct.declare> <set name='m_user' type='struct'> <struct type='s_user'> <string>Manuel</string> <string>Perico</string> <number>33</number> <string>ES</string> <string>Barcelona</string> </struct> </set> <println><m_user.city /></println> </body> </xsql-script>
Barcelona
var mObjPerson = { "firstName": "Carlos", "lastName": "González", "country": "España", "code": 25701, "city": "Barcelona" } console.log(mObjPerson.city); console.log(mObjPerson["city"]); //expression x = "city"; console.log(mObjPerson[x]);
Barcelona
Barcelona
Barcelona
1.1.2 Add/Delete properties
In XSQL Script
it's not possible to add
or remove
a property from the declared structure.
The properties of an object can be removed or new ones can be added. The following example will show how to add and remove different properties of the user object
The delete
operator deletes both the property and its value.
<xsql-script> <!-- Not possible to add or remove a property. --> </xsql-script>
var person = { firstName: 'Carlos', lastName: 'González', code: 25701, country: 'España', city: 'Barcelona' }; console.log(person); //Person before object // Result of Add person.academicTitle = "Engineer"; //Add object property console.log(person); //Person after object // Result of Delete delete person.city; //Delete object property console.log(person); //Person after object
//Person before object
{
"firstName": "Carlos",
"lastName": "González",
"country": "España",
"code": 25701,
"city": "Barcelona"
}
// Result of Add
{
"firstName": "Carlos",
"lastName": "González",
"country": "España",
"code": 25701,
"city": "Barcelona",
"academicTitle": "Engineer"
}
// Result of Delete
{
"firstName": "Carlos",
"lastName": "González",
"country": "España",
"code": 25701,
"academicTitle": "Engineer"
}
1.2 Object Methods
Methods are the functions that allow the object to do something or have something done to it. The difference between a function and a method is that, in a function, it's a self-contained unit of declarations and a method is attached to an object.
Methods can be useful to show the content of the object on the screen or to perform complex mathematical operations.
<xsql-script> <body> <set name='f_name'>Carlos</set> <set name='l_name'>González</set> <set name='code'>25701</set> <struct.declare type='s_user'> <field name='firstname' type='string' /> <field name='lastname' type='string' /> <field name='code' type='integer' /> <field name='lives' type='string'><get_user /></field> </struct.declare> <function name='get_user'> <body> <return> <set name='city'>Barcelona</set> <set name='country'>España</set> </return> </body> </function> <set name='m_user' type='struct'> <struct type='s_user'> <string><f_name /></string> <string><l_name /></string> <number><code /></number> <get_user /> </struct> </set> <println>He is <m_user.firstname /> <m_user.lastname /> and he lives in <get_user />.</println> </body> </xsql-script>
He is Carlos González and he lives in [Barcelona,España].
var user = { firstName: 'Carlos', lastName: 'González', code: 25701, country: 'España', city: 'Barcelona', lives = function() { return this.city + ", " + this.country; } }; console.log('He is ' + user.firstName, user.lastName + ' and he lives in ' + user.lives() +'.');
He is Carlos González and he lives in Barcelona, España.
2 Object
In JavaScript, almost "everything" is an object.
- Booleans can be objects (if defined with the new keyword)
- Numbers can be objects (if defined with the new keyword)
- Strings can be objects (if defined with the new keyword)
- Dates are always objects
- Maths are always objects
- Regular expressions are always objects
- Arrays are always objects
- Functions are always objects
- Objects are always objects
All JavaScript values, except primitives, are objects.
2.1 Server side script Primitives
A primitive value is a value that has no properties or methods.
A primitive data type is data that has a primitive value.
Server side cript defines 5 types of primitive data types:
- string
- number
- boolean
- null
- undefined
<xsql-script> <body> <!--In XSQL all reference to an Object --> <!-- string is similar to string in server side script --> <set name='m_name' type='string'>Manuel</set> <!-- Class of type number are similar to number in server side script --> <set name='m_order' type='integer'>2</set> <set name='m_float' type='float'>2.03</set> <set name='m_decimal' type='decimal'>2.01</set> <!-- Class Boolean are similar to boolean in server side script --> <set name='m_is_false' type='boolean'><false /></set> <!-- Null is similar to null and undefined in server side script --> <null name='m_null' /> </body> </xsql-script>
// var mStrName = 'Manuel'; var mIntNumber = 2.03; var mBoolFalse = false; // null var mIsNull = null; // undefined var mIsUndefined = undefined;
2.2 Single and multiple values objects
<xsql-script> <body> <!-- single value --> <set name='m_name' type='string'>Manuel</set> <println><m_name /></println> <println /> <!-- multiple value --> <struct.declare type='point3d'> <field name='x' type='integer' /> <field name='y' type='integer' /> <field name='z' type='integer' /> </struct.declare> <set name='m_point'> <struct type='point3d'> <number>2</number> <number>4</number> <number>6</number> </struct> </set> <println><m_point /></println> </body> </xsql-script>
Manuel
struct.point3d
{
point3d.x (4 - INTEGER java.lang.Integer) = 2
point3d.y (4 - INTEGER java.lang.Integer) = 4
point3d.z (4 - INTEGER java.lang.Integer) = 6
}
// single value var mStrName = 'Manuel'; console.log(mStrName); console.log(""); // multiple value var mObjPoint3d = { x : 2, y : 4, z : 6 } console.log(mObjPoint3d);
Manuel
{
"x": 2,
"y": 4,
"z": 6
}
2.3 Some examples of creating objects
Literally
var mObjPoint3d = {x : 2, y : 4,z : 6} console.log(mObjPoint3d);
{
"x": 2,
"y": 4,
"z": 6
}
Spaces and line breaks are not important. An object definition can span multiple lines
var mObjPoint3d = { x : 2, y : 4, z : 6 } console.log(mObjPoint3d);
{
"x": 2,
"y": 4,
"z": 6
}
Creating with new Object() and adding properties
var mObjPoint3d = new Object(); mObjPoint3d.x = 2; mObjPoint3d.y = 4; mObjPoint3d.z = 6; console.log(mObjPoint3d);
{
"x": 2,
"y": 4,
"z": 6
}
2.4 Objects are Mutable
Objects and structs are addressed by reference, not by value.
<xsql-script> <body> <struct.declare type='s_car'> <field name='brand' type='string' /> <field name='model' type='string' /> <field name='power' type='integer' /> </struct.declare> <set name='m_car1' type='struct'> <struct type='s_car'> <string>Siat</string> <string>Elebro</string> <number>100</number> </struct> </set> <set name='m_car2'><m_car1 /></set> <set name='m_car2.model'>Marbillo</set> <println>Car1: </println> <println><m_car1 /></println> <println>Car2: </println> <println><m_car2 /></println> </body> </xsql-script>
Car1:
struct.s_car
{
s_car.brand (1 - CHAR java.lang.String) = Siat
s_car.model (1 - CHAR java.lang.String) = Marbillo
s_car.power (4 - INTEGER java.lang.Integer) = 100
}
Car2:
struct.s_car
{
s_car.brand (1 - CHAR java.lang.String) = Siat
s_car.model (1 - CHAR java.lang.String) = Marbillo
s_car.power (4 - INTEGER java.lang.Integer) = 100
}
var mObjCar = { brand : "Siat", model : "Elebro", power : 300 } var mObjCar2 = mObjCar; mObjCar.model = "Marbillo"; // This will change both mObjCar2.model and mObjCar.model console.log("Car1:"); console.log(mObjCar2); console.log("Car2:"); console.log(mObjCar);
Car1:
{
"model": "Marbillo",
"power": 300,
"brand": "Siat"
}
Car2:
{
"model": "Marbillo",
"power": 300,
"brand": "Siat"
}
2.5 Using JSON.stringify()
Many of server side script objects can be stringified (converted to a string) with the JavaScript function JSON.stringify():
var mObjCar = { brand:"Fiot", model: "Penda", power: 50 }; var mStrCar = JSON.stringify(mObjCar); console.log(mStrCar);
{"brand":"Fiot","model":"Penda","power":50}
2.5.1 Stringify Dates
JSON.stringify converts dates into strings:
var mObjCar = { brand : "Morcedes", buy_date : new Date() }; var mStrCar = JSON.stringify(mObjCar); console.log(mStrCar);
{"brand":"Morcedes","buy_date":"2020-12-28T11:44:55.258Z"}
JSON.stringify remove Java objects from conversion to String:
var mObjCar = { brand : "Morcedes", buy_date : new Ax.util.Date() }; var mStrCar = JSON.stringify(mObjCar); console.log(mStrCar); // you can use .toString() before to display mObjCar = { brand : "Morcedes", buy_date : new Ax.util.Date().toString() }; var mStrCar = JSON.stringify(mObjCar); console.log(mStrCar);
{"brand":"Morcedes"}
{"brand":"Morcedes","buy_date":"Mon Dec 28 12:59:04 CET 2020"}
2.5.2 Stringify Functions
JSON.stringify will not stringify functions.
var mObjCar = { brand : "Fiot", age : function () {return 22;} }; var mStrCar = JSON.stringify(mObjCar); console.log(mStrCar);
{"brand":"Fiot"}
This can be "fixed" if you convert the functions into strings before stringifying
var mObjCar = { brand : "Fiot", age:function () {return 22;} }; mObjCar.age = mObjCar.age.toString(); var mStrCar = JSON.stringify(mObjCar); console.log(mStrCar);
{"brand" : "Fiot", "age" : "function () {return 22;}"}
2.5.3 Stringify Arrays
var mArrFruits = ["Apple", "Orange", "Kiwy", "Grape"]; var mStrFruits = JSON.stringify(mArrFruits); console.log(mStrFruits)
["Apple","Orange","Kiwy","Grape"]
3 Object Constructors
<xsql-script> <body> <struct.declare type='s_car'> <field name='brand' type='string' /> <field name='model' type='string' /> <field name='age' type='integer' /> <field name='power' type='integer' /> </struct.declare> <set name='m_car1' type='struct'> <struct type='s_car'> <string>Soat</string> <string>Lacon</string> <number>2</number> <number>150</number> </struct> </set> <set name='m_car2' type='struct'> <struct type='s_car'> <string>Feat</string> <string>Rally</string> <number>23</number> <number>58</number> </struct> </set> <println>Car1: </println> <println><m_car1 /></println> <println>Car2: </println> <println><m_car2 /></println> </body> </xsql-script>
Car1:
struct.s_car
{
s_car.brand (1 - CHAR java.lang.String) = Soat
s_car.model (1 - CHAR java.lang.String) = Lacon
s_car.age (4 - INTEGER java.lang.Integer) = 2
s_car.power (4 - INTEGER java.lang.Integer) = 150
}
Car2:
struct.s_car
{
s_car.brand (1 - CHAR java.lang.String) = Feat
s_car.model (1 - CHAR java.lang.String) = Rally
s_car.age (4 - INTEGER java.lang.Integer) = 23
s_car.power (4 - INTEGER java.lang.Integer) = 58
}
function Car(brand, model, age, power) { this.brand = brand; this.model = model; this.age = age; this.power = power; } // Objects of the same type are created by calling the constructor function with the new keyword var car1 = new Car("Soat", "Lacon", 2, 150); var car2 = new Car("Feat", "Rally", 23, 58); console.log("Car1:"); console.log(car1); console.log("Car2:"); console.log(car2);
Car1:
{
"model": "Lacon",
"power": 150,
"brand": "Soat",
"age": 2
}
Car2:
{
"model": "Rally",
"power": 58,
"brand": "Feat",
"age": 23
}
3.1 The this Keyword
This is used in server side script to refer to the object, class, or other entity of which the currently running code is a part. The entity referred to by these keywords thus depends on the execution context (such as which object is having its method called). Different programming languages use these keywords in slightly different ways. In languages where a keyword like "this" is mandatory, the keyword is the only way to access data and methods stored in the current object. Where optional, they can disambiguate variables and functions with the same name.
3.2 Adding a Property to an Object
var mObjCar1 = { model : "Lacon", power : 150, brand : "Soat" } mObjCar1.age = 2; console.log(mObjCar1);
{
"model": "Lacon",
"power": 150,
"brand": "Soat",
"age": 2
}
3.3 Adding a Property to an Object
var mObjCar1 = { model : "Lacon", power : 150, brand : "Soat" } mObjCar1.logo = function () { return this.brand+ " " + this.model; }; console.log(mObjCar1.logo());
Soat Lacon
3.4 Built-in server side script Constructors
Server side script has built-in constructors for native objects:
var mObj1 = new Object(); // A new Object object var mStr1 = new String(); // A new String object var mNum1 = new Number(); // A new Number object var mBool1 = new Boolean(); // A new Boolean object var mArr1 = new Array(); // A new Array object var mReg1 = new RegExp(); // A new RegExp object var mFun1 = new Function(); // A new Function object var mDate1 = new Date(); // A new Date object var mObj2 = {}; // new object var mStr2 = ""; // new primitive string var mNum3 = 0; // new primitive number var mBool3 = false; // new primitive boolean var mArr3 = []; // new array object var mReg3 = /()/ // new regexp object var mFun3 = function(){}; // new function object
Strings, numbers and booleans are created as primitives.
But strings, numbers and booleans can also be created as objects using the new keyword
var mStrName = "John"; //string primitive var mStrName = new String("John"); // String Object var mNumberX = 123; // String primitive var mNumberX = new Number(123); // String primitive var mBoolX = false; // Boolean primitive var mBoolX= new Boolean(false); // Boolean object