Untill they decide to add a new sub-forum, I will post here some things you may want to know about javascript.
We don't really know about how the engine will work, now they will support plug-ins and a lot of features, but here are some 'tricks' to allow people to get a llitle more confident about using javascript, if they came from ruby and only ruby.
Unlike ruby, javascript looks a lot more like c.
If you already look at the code fragments, you will se than I'm using a lot of curly brackets, and that is because javascript does not have 'if x then end' but 'if (x) { }'
Oh, don't worry is not as bad when you use it some time, but the migration will be a pain...
First of all, you have to remember than javascript does not have integers, like ruby
so, 3 / 2, is 1,5 always (you don't need to put a .0 like 2.0 or 3.0)
So, you still want to do integer division ?
You may prefer one of this two methods:
// By the way, this are line comments, like the '#' in rubyMath.floor(x / y) // Will give the integer part (for example 2.0 for 2.5) // This will work with any kind of integer up to 2^54 ~~(x / y) // A faster way, but only for numbers up to 2^32The second one should be used anytime you can (if you are using the standard damage limit like 99999 this should work fine almost always)
Aliasing is also possible with the new rpg maker, albeit a bit more obscure:
var oldFunctionAlias = oldFunctionoldFunction = function () { // some code ... oldFunctionAlias.apply(this, arguments); // more code}You can also change methods of "classes" (even if, there are no concept of "class" in javascript now, more later)
var oldFunctionAlias = AClass.prototype.oldFunction // "prototype?, don't worry later I will talk about this"AClass.prototype.oldFunction = function () { // ...}You should ALWAYS ALWAYS ALWAYS declare new variables with 'var':
var x = 23I told you there are no classes in javascript (Unless of course, this version follows the ES6 standard, with I doubt)
And even then, there are no "truly" classes there.
Here, objects are all prototypes, and as such, can extend from other objects.
So, here is like, if you like to "extend" something, you copy it, and then change the things you need.
While in ruby this is also possible (because open classes and monkey patching allow us) Now is the core concept, and not an add-on.
How do I make a class now? Well, here we use functions as constructors (Imagine, you define a class with only its "initialize" method:
function AClass(arg1, arg2) { this.x = 3; // 'this' is the equivalent of 'self', javascript does not have self};Now, if you want to create a new instance, you just do:
var a_var = new AClass(1, 2);Not so hard, but here comes the tricy part.
Do you want to extend it now ? like ASubclass? let me show you how you need to do it:
function ASubclass() { // code... AClass.apply(this, arguments); // You can call this "how javascript does 'super' and 'alias' " // code...};// Here comes the prototype againASubclass.prototype = Object.create(AClass.prototype);ASubclass.prototype.constructor = ASubclass;Now, let me explain this:
A prototype is an especial value in all functions, its the object wich is passed as "this" when you call that function.
You can use any object really, like a literal object: { x: 2, y: 3 } (more on this later) or a class, like you just saw there.
You need to clone a prototype from the class thou.
Say goodbye to hashes, and hello to bare objects.
Javascript does not have hashes (called normally maps in other language) (well, unless you use the class Map).
The old way to get and set elements from an array is still almost like ruby
var an_array = [1, 2, 3]an_array[2] // Will give you 3, like rubyan_array['2'] // But this will also give 3The problem here is, objects only carry properties as strings, so everything is converted into one.
So, unlike ruby '2' and 2 does not store different values on an object.
Oh, and if you just think that is enough, just considered this:
an_item.a_varis really a short (an probably faster) way to write:
an_item['a_var']Also, multi dimensional arrays are not allowed, well, not like ruby:
an_item[2, 3] // is invalidan_item[2][3] // is valid, but it also means another thing technicallyBelive me, even when I am planning to create my own language (that's why I am not very active) and kinda like javascript (its not clear my love-hate relation with this language)
I will indeed miss ruby... for sure.
Hope this mini lessons help you get a warm up from the upcoming scripting, if you are interesting.
if you still want more, you can learn it from some places:
Hope this post will help you decide to learn even more.
Remember to have fun, and stay awesome!
EDIT:
You may also want to know:
Doing mixins:
function mixin(proto /* , more args */) { for (var i = 1; i < arguments.legth; ++i) { var obj = arguments; for (p in obj) { if (obj.hasOwnProperty(p) && !proto.hasOwnProperty(p)) { proto[p] = obj[p]; } } } };You just use like:
mixin(Class.prototype, Mixin1, Mixin2, ...);Arrays, and regexp are almost the same:
var array = [1, 2, 3];
Som platforms now support string interpolation:
console.log("I have my variable 'x' with the value of %{x}");You can do a -format like- if you prefer with this:
String.prototype.format= function (o) {return this.replace(/{([^{}]*)}/g,function (a, {var r = o;return typeof r === 'string' || typeof r === 'number' ? r : a;});};And use it like:
We don't really know about how the engine will work, now they will support plug-ins and a lot of features, but here are some 'tricks' to allow people to get a llitle more confident about using javascript, if they came from ruby and only ruby.
Unlike ruby, javascript looks a lot more like c.
If you already look at the code fragments, you will se than I'm using a lot of curly brackets, and that is because javascript does not have 'if x then end' but 'if (x) { }'
Oh, don't worry is not as bad when you use it some time, but the migration will be a pain...
First of all, you have to remember than javascript does not have integers, like ruby
so, 3 / 2, is 1,5 always (you don't need to put a .0 like 2.0 or 3.0)
So, you still want to do integer division ?
You may prefer one of this two methods:
// By the way, this are line comments, like the '#' in rubyMath.floor(x / y) // Will give the integer part (for example 2.0 for 2.5) // This will work with any kind of integer up to 2^54 ~~(x / y) // A faster way, but only for numbers up to 2^32The second one should be used anytime you can (if you are using the standard damage limit like 99999 this should work fine almost always)
Aliasing is also possible with the new rpg maker, albeit a bit more obscure:
var oldFunctionAlias = oldFunctionoldFunction = function () { // some code ... oldFunctionAlias.apply(this, arguments); // more code}You can also change methods of "classes" (even if, there are no concept of "class" in javascript now, more later)
var oldFunctionAlias = AClass.prototype.oldFunction // "prototype?, don't worry later I will talk about this"AClass.prototype.oldFunction = function () { // ...}You should ALWAYS ALWAYS ALWAYS declare new variables with 'var':
var x = 23I told you there are no classes in javascript (Unless of course, this version follows the ES6 standard, with I doubt)
And even then, there are no "truly" classes there.
Here, objects are all prototypes, and as such, can extend from other objects.
So, here is like, if you like to "extend" something, you copy it, and then change the things you need.
While in ruby this is also possible (because open classes and monkey patching allow us) Now is the core concept, and not an add-on.
How do I make a class now? Well, here we use functions as constructors (Imagine, you define a class with only its "initialize" method:
function AClass(arg1, arg2) { this.x = 3; // 'this' is the equivalent of 'self', javascript does not have self};Now, if you want to create a new instance, you just do:
var a_var = new AClass(1, 2);Not so hard, but here comes the tricy part.
Do you want to extend it now ? like ASubclass? let me show you how you need to do it:
function ASubclass() { // code... AClass.apply(this, arguments); // You can call this "how javascript does 'super' and 'alias' " // code...};// Here comes the prototype againASubclass.prototype = Object.create(AClass.prototype);ASubclass.prototype.constructor = ASubclass;Now, let me explain this:
A prototype is an especial value in all functions, its the object wich is passed as "this" when you call that function.
You can use any object really, like a literal object: { x: 2, y: 3 } (more on this later) or a class, like you just saw there.
You need to clone a prototype from the class thou.
Say goodbye to hashes, and hello to bare objects.
Javascript does not have hashes (called normally maps in other language) (well, unless you use the class Map).
The old way to get and set elements from an array is still almost like ruby
var an_array = [1, 2, 3]an_array[2] // Will give you 3, like rubyan_array['2'] // But this will also give 3The problem here is, objects only carry properties as strings, so everything is converted into one.
So, unlike ruby '2' and 2 does not store different values on an object.
Oh, and if you just think that is enough, just considered this:
an_item.a_varis really a short (an probably faster) way to write:
an_item['a_var']Also, multi dimensional arrays are not allowed, well, not like ruby:
an_item[2, 3] // is invalidan_item[2][3] // is valid, but it also means another thing technicallyBelive me, even when I am planning to create my own language (that's why I am not very active) and kinda like javascript (its not clear my love-hate relation with this language)
I will indeed miss ruby... for sure.
Hope this mini lessons help you get a warm up from the upcoming scripting, if you are interesting.
if you still want more, you can learn it from some places:
Hope this post will help you decide to learn even more.
Remember to have fun, and stay awesome!
EDIT:
You may also want to know:
Doing mixins:
function mixin(proto /* , more args */) { for (var i = 1; i < arguments.legth; ++i) { var obj = arguments; for (p in obj) { if (obj.hasOwnProperty(p) && !proto.hasOwnProperty(p)) { proto[p] = obj[p]; } } } };You just use like:
mixin(Class.prototype, Mixin1, Mixin2, ...);Arrays, and regexp are almost the same:
var array = [1, 2, 3];
Code:
var regexp = /(.*?)[a-z]$/i;
console.log("I have my variable 'x' with the value of %{x}");You can do a -format like- if you prefer with this:
String.prototype.format= function (o) {return this.replace(/{([^{}]*)}/g,function (a, {var r = o;return typeof r === 'string' || typeof r === 'number' ? r : a;});};And use it like:
Code:
"Hello world, mi age is {age}".format({ age: 25 })