Hi people!
Well, I'm studying JavaScript and I'm learning about classes in ES6 syntax.
That would be something like that, right?
But I saw in rpg_core.js that the classes are made in a different way(ES5 syntax, right?)
So if I want to code this with the es6 syntax, I have to do something like this?
Also, there are some methods in rpg core, like this one:
Why they used a prototype here? The Number is part of the class Js Extensions?
Or there is a class for Numbers that I don't see?
Because, in my knowledge, a prototype is used for a function to allow it being used by other classes, right(inheritance)?
I have understood this prototype:
Because I can see that is inside the class of Cache Entry.
But what about Number?
What i'm missing?
[[[EDIT]]]
Well according to this:
https://www.w3schools.com/js/js_object_prototypes.asp
It seems that "Number" is an object inherit from Object.prototype like array object, date object etc?
And in this method of RPG core that I listed above:
Number.prototype.clamp The clamp is a new property of the Number that has been added.
So, in this case, it is modifying a standard javascript object(Number) adding a new property? That's what happening?
Well, I'm studying JavaScript and I'm learning about classes in ES6 syntax.
That would be something like that, right?
JavaScript:
class Pessoa {
constructor (nome, cpf) {
this.nome = nome
this.cpf = cpf
}
getNome () {
return this.nome
}
getCpf () {
return this.cpf
}
}
But I saw in rpg_core.js that the classes are made in a different way(ES5 syntax, right?)
JavaScript:
/**
/**
* The resource class. Allows to be collected as a garbage if not use for some time or ticks
*
* @class CacheEntry
* @constructor
* @param {ResourceManager} resource manager
* @param {string} key, url of the resource
* @param {string} item - Bitmap, HTML5Audio, WebAudio - whatever you want to store in the cache
*/
function CacheEntry(cache, key, item) {
this.cache = cache;
this.key = key;
this.item = item;
this.cached = false;
this.touchTicks = 0;
this.touchSeconds = 0;
this.ttlTicks = 0;
this.ttlSeconds = 0;
this.freedByTTL = false;
}
/**
* frees the resource
*/
CacheEntry.prototype.free = function (byTTL) {
this.freedByTTL = byTTL || false;
if (this.cached) {
this.cached = false;
delete this.cache._inner[this.key];
}
};
}
So if I want to code this with the es6 syntax, I have to do something like this?
JavaScript:
/**
* The resource class. Allows to be collected as a garbage if not use for some time or ticks
*
* @class CacheEntry
* @constructor
* @param {ResourceManager} resource manager
* @param {string} key, url of the resource
* @param {string} item - Bitmap, HTML5Audio, WebAudio - whatever you want to store in the cache
*/
class CacheEntry{
constructor(cache, key, item){
this.cache = cache;
this.key = key;
this.item = item;
this.cached = false;
this.touchTicks = 0;
this.touchSeconds = 0;
this.ttlTicks = 0;
this.ttlSeconds = 0;
this.freedByTTL = false;
}
/**
* frees the resource
*/
CacheEntry(byTTL){
this.freedByTTL = byTTL || false;
if (this.cached) {
this.cached = false;
delete this.cache._inner[this.key];
}
}
}
Also, there are some methods in rpg core, like this one:
JavaScript:
//-----------------------------------------------------------------------------
/**
* This is not a class, but contains some methods that will be added to the
* standard Javascript objects.
*
* @class JsExtensions
*/
function JsExtensions() {
throw new Error('This is not a class');
}
/**
* Returns a number whose value is limited to the given range.
*
* @method Number.prototype.clamp
* @param {Number} min The lower boundary
* @param {Number} max The upper boundary
* @return {Number} A number in the range (min, max)
*/
Number.prototype.clamp = function(min, max) {
return Math.min(Math.max(this, min), max);
};
Why they used a prototype here? The Number is part of the class Js Extensions?
Or there is a class for Numbers that I don't see?
Because, in my knowledge, a prototype is used for a function to allow it being used by other classes, right(inheritance)?
I have understood this prototype:
JavaScript:
CacheEntry.prototype.free
But what about Number?
What i'm missing?
[[[EDIT]]]
Well according to this:
https://www.w3schools.com/js/js_object_prototypes.asp
It seems that "Number" is an object inherit from Object.prototype like array object, date object etc?
And in this method of RPG core that I listed above:
Number.prototype.clamp The clamp is a new property of the Number that has been added.
So, in this case, it is modifying a standard javascript object(Number) adding a new property? That's what happening?