Object.prototype.method doesn't work

67 views Asked by At

i've created this prototype method for Object, but it stops all my code, when i test it, it works for some objects list but followings codes don't work. Thank you to help me.

Object.prototype.toMapIt = function(){
  var arrayFinal = [];
  for(var key in this){
    if(key != "toMapIt"){
      var array = [];
      array.push(key);
      array.push(this[key]);
      arrayFinal.push(array);
    }
  }
  return new Map(arrayFinal);
}
//Using the method
var object = {key1 : value1, key2 : value2};

var map = object.toMapIt();
1

There are 1 answers

0
Jordan Chevalier On

So i don't have problem with this :

var toMap = function(object){
  var finalArray = [];
  for(var key in object){
      var array = [];
      array.push(key);
      array.push(object[key]);
      finalArray.push(array);
  }
  return new Map(finalArray);
}


$(function(){
  
  var test = {ko : "jiioji", opop : "ijoijoij"};

  console.log(test);
  console.log(toMap(test));

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>See your console</p>
<p>I've made this function because i have big objects, and i want to use some map properties on this objects</p>