> Testcase 2:
> Now doing it with two js files.
>
> main.js
> =======
> const common = require("common");
> const arrLanguages = ["de"];
>
> exports.main = function (options, callbacks) {
> common.checkArray(arrLanguages);
> }
> common.js
> ==========
> exports.checkArray = function(arr) {
> console.debug(arr.constructor);
> if (arr.constructor === Array) {
> console.debug("It's an Array");
> } else {
> console.debug("Damn, no Array");
> }
> };
That's because the JavaScript context is different, so the globals are
different as well. The `Array` object from `main.js` is not the same of
`common.js`.
So, if you create an array in `main.js`, the `constructor` will be the
`Array` of `main.js`.
If you're familiar with web development, you have the same situation
across frames. That's the main reason because in ES5 you have
`Array.isArray` method:
exports.checkArray = function(arr) {
if (Array.isArray(arr)) {
console.debug("It's an Array");
} else {
console.debug("Damn, no Array");
}
}
Hope it helps.
> My reusable functions are put together into one javascript file.
> I don't like to copy these function into every other javascript file.
You don't have. But if you compare two constructors from different
JavaScript context they cannot be the same. They're different objects.
> In my opinion it's wrong to say that the constructor Array is not equal
> to another Array in a different scope.
It's not a matter of scope, but context. This is how JavaScript works.
> An Array is an Array and an Object is an Object.
Sure, in fact you can check if an Array is an Array using
`Array.isArray` method. It's not something introduced by Add-on SDK,
it's a pure JavaScript method (ES5) that was created for exactly this
purpose.
> And don't tell me the ugly workarounds like checking if something has a
> length or making a string compare is a solution.
I don't. In fact I suggested to you the right method to use, no workarounds.
> The fastest and explicit was is to compare the constructors.
In JavaScript it's not reliable as method, because as you saw you can
end up to compare different objects.
Have a look here:
<http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/>
> But may be you can give me a good reason why an Array from main.js is
> not equal to an Array from another js within the Addon SDK.
As the link described, this behavior is not specific to Add-on SDK, this
is related to JavaScript in general. In Add-on SDK every modules has its
own context � like different iframes.