Why match returns an object - JAVASCRIPT

Asked

Viewed 205 times

4

I was always one of those who understood the functions and applied, but I want to understand, why this:

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 moral of a function return an object? Is there a justification for this application? Help me to understand a little this concept of object in javascript, I understand that object is an instance of a class, with this it receives properties of the class that was instantiated in it.. If you want to prescribe a place to read more about it, thank you also!

  • 1

    Well, first of all I recommend reading this W3C article: http://www.w3schools.com/js/js_arrays.asp

  • 2

    To complete Marcelo’s comment. The method match returns a Array (as there can be multiple results). One Array is an object.

  • rsrs, it’s crazy this, an Array is in its essence an object that may or may not contain several other objects of different types.

  • 1

    Good afternoon, did any of the answers solve your problem? If yes please mark it as "Correct". If you do not say what is missing. Thank you.

  • Yes, there are several questions that I always forget to mark as the best answer.. But I will try to correct this, thank you.

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 Type Array).

This is actually a part of the language that is "incomplete". To complete this flaw, in Ecmascript version 5, a new method has been implemented to help with this assignment: Array.isArray(var)

This new method, available in modern browsers (IE9+) allows you to ask questions. That is to say:

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. Joining this code detects if the browser supports the .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 moral 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, we need to learn and accept that this is so :)

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

1

.match can return both Array how much null and both are "considered" object by Javascript, on the line:

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

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

To check whether a object is a array, you can do it:

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;   
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.