Why match returns an object - JAVASCRIPT

4

I've always been one of those who understood the functions and applied, but I want to understand why:

var a = "__myseld=ej232;Nome=Alexandre";
var as = a.split(";");
var x = 0;
while (x<5) {
    alert("type of de as(sem match): " + typeof(as[x]));
    bs = as[x].match(/a/i);
    alert("typeof de bs(match): " + typeof(bs));
    if (as[x].match(/nome/i) != null){
        
        alert("achei!: " + as[x]);
        x=6;
    }
    else {
     x+=1;   
    }
}

What morality of a function does an object return? Is there a justification for this application? I'm trying to get a class that has a class that has been instantiated in javascript. If you want to prescribe a place to read more about it, thank you too!

    
asked by anonymous 07.01.2015 / 13:13

2 answers

7

In JavaScript, both objects and arrays (among others) are ... objects. This subject has been debated a lot, and mainly because it is often important to distinguish exactly between array and objects (I am referring to Type Object and Array ). p>

This is actually a part of the language that is "incomplete". To complete this flaw, in version 5 of EcmaScript, a new method was implemented to help this destination: Array.isArray(var)

This new method, available in modern browsers (IE9 +) allows to clear the doubts. That is:

Array.isArray({}) // false
Array.isArray([]) // true

Before this was possible, there were different, more laborious ways to come to the same conclusion. One of them is suggested in MDN as Polyfil. Adding this code it detects if the browser supports .isArray() and adds the method to Array if necessary:

if (!Array.isArray) {
  Array.isArray = function(arg) {
    return Object.prototype.toString.call(arg) === '[object Array]';
  };
}

In the background an object is Array if the following is true:

Object.prototype.toString.call(arg) === '[object Array]'

The morality of a function / method returning an object is because Arrays and Objects in JavaScript are instances of Object . This is more or less like grammar, you have to learn and accept that it is like this:)

Example (and another way to distinguish Arrays from Objects):

var a = [];
var o = {};
a instanceof Array   // true
o instanceof Array   // false <---

a instanceof Object  // true
o instanceof Object  // true
    
07.01.2015 / 15:15
1

.match can return both Array and null and both are "considered" object by Javascript, in line:

 bs = as[x].match(/a/i);

returns null in the first loop and array in the second, typeof will report that both are object .

To check if a object is a array , you can do this:

if (bs instanceof Array) {
    //Seu código
}

A test:

var a = "__myseld=ej232;Nome=Alexandre";
var as = a.split(";");
var x = 0;
while (x<5) {
    alert("type of de as(sem match): " + typeof(as[x]));
    bs = as[x].match(/a/i);

    if (bs instanceof Array) {
        alert("bs é array");
        console.log(bs);
    } else if (bs === null) {
        alert("bs é NULL");
    }

    if (as[x].match(/nome/i) != null){
        
        alert("achei!: " + as[x]);
        x=6;
    }
    else {
     x+=1;   
    }
}
    
07.01.2015 / 13:40