Win an Ipad
  Developer   Tutorials   Beginners’ tutorial ...   Moscrif for JavaScri ...
Lesson 9

Moscrif for JavaScript developers

Moscrif language uses an extended and a modified version of the ECMAScript (JavaScript version 1.x). More detailed information about the ECMAScript's extentions and modifications are available in this document.

Code embeding

Moscrif uses new keyword include to embed an external source code. It's also possible to use #include instead of include, which should be appreciated by a C, C++ and-or Objective-C developers.

JavaScript Moscrif
include "app://file.js";

You can specify 3 different places of a source code:

  • "app://" - points to a root application folder, it's read-only
  • "lib://" - points to a framework (libraries) folder, it's read-only
  • "dat://" - points to a special application folder with read/write permissions

Note: "app://" and "lib://" folders are read-only. Writable file must be stored in "dat://" folder according to mobile operation system' security policy.

Logging

Console logging is the easiest way how to track the code while debugging. You can write any string value into the console by using one like code snippet.

JavaScript Moscrif

document.write("message");

cosole.log("message");

console.printf("message\n");

console.printf("formatted %d\n", 10);

console << "message\n";

Classes

Moscrif uses first-level classes with a constructor(s) and a properties and also supports the class inheritance. Moscrif's classes are similar to C++, C# or Java classes.

JavaScript Moscrif

function Hello(name) {

  this.name = name;

}

hello.prototype.say = function() {

 return "Hello " + this.name;

}

 

var test = new Hello("world");

document.write(test.say());

class Hello {

  function this(name) {

    this.name = name;

  }

  function say() {

    return "Hello " + this.name;

  }

}

 

var test = new Hello("world");

console << test.say();

Variables and Constants

Moscrif uses the variables the exactly same way as JavaScript does with an addition of constants that were implemented.

JavaScript Moscrif

var str = "String";

var num = 1;

// constants are not implemented

var str = "String";

var num = 1;

const MAX_NUM = 100;

const MASK_OK = 1 << 20;

const STRING_CONST = "token";

Default parameters

Even though the pure JavaScript supports default parameters, its usage is not as straightforward as default parameters from other languages. Therefore, Moscrif uses these the same way as Java or C++ does.

JavaScript Moscrif

function hello(name, greeting) {

 greeting = greeting || "Hello";

 return greeting + ", " + name;

}

 

console.log(hello("John"));

// Hello, John

 

console.log(hello("John", "Ahoy"));

// Ahoy, John

function hello(name, greeting = "Hello") {

return greeting + ", " + name;

}

console << hello("John") << "\n";

// Hello, John

 

console << hello("John", "Ahoy") << "\n";
// Ahoy, John

Exceptions

Exceptions are used to trap the errors in the application preventing the app for crashing.

JavaScript Moscrif

throw new Error("Invalid argument");

// ... or ...

throw "Invalid argument";

throw "Invalid argument";

Class extentions

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

JavaScript Moscrif

String.prototype.hello = function()
{
  return "Hello, " + this.toString();
}

var name = "John";
document.write(name.hello());
// Hello, John.

String.hello = function()
{
  return "Hello, " + this.toString();
}

var name = "John";
console << name.hello() << "\n";
// Hello, John.

Symbols

Moscrif introduces new type called symbols - auto enum values declared in place of usage. Internally symbols are numbers associated to a names. The symbol starts with # character and can contain alphabets, numbers, underscore, plus or minus characters.

JavaScript Moscrif

// symbols aren't implemented

var align = #left;

console << align << "\n";

if (align == #right)

 console << "Mission imposible\n";

if (align == #left || align == #right)

  console << "Alignment is valid\n";

Numbers, Integers and Floats

JavaScript has only one numeric type represented by class Number. Moscrif recognizes Integer and Float types separately to be more convenient for mobile development. 

JavaScript Moscrif

// JavaScript has Number only

var num1 = 10;

var num2 = 10 / 3;

num1 instance of Number; // true

num2 instance of Number; // true

// Moscrif has Interers and Floats

var int1 = 10;

var int2 = 10 / 3; 

var int3 = 10 % 3;

int1 instanceof Integer; // true

int2 instanceof Integer; // true

int2 instanceof Float; // false

int2 instanceof Integer; // true

 

var flt1 = 1.0;

var flt2 = 10 / 3.0;

flt1 instanceof Float; // true

flt1 instanceof Integer; // false

flt2 instanceof Float; // true

Strings and Chars

Moscrif bring characters from C/C++/ObjectiveC/Java world. Character is an integer that represents exactly one (unicode) character. Practically 'A' is 65, as it's known from many other languages. JavaScript's strings can start with both " and ' characters. Moscrif's string can start with " or `, but ' is used to specity character (an integer) not a string!

JavaScript Moscrif

// use both ' and " for strings

var str1 = "Text"; 

var str2 = 'Text';

str1 instance of String; // true

str2 instance of String; // true

// use " and ` for strings, ' for chars

var str2 = "Text";

var str2 = 'Text'; // Error: Bad syntax : Expecting a closing single quote

str1 instance of String; // true

 

// chars are integers!

 

var ch1 = 'A'; // integer

ch1 instance of String; // false

ch1 instance of Intger; // true

 

 Creating a high scor ...   Moscrif for JavaScri ...   Changing device' ori ... 
Tutorial details and chapters
Technology:
iOS, Android
Difficulty:
Beginner
Completion:
35 min
Beginners’ tutorial series chapters